Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用带有.xib文件和自动布局的自定义UIView调整UIImageView的大小_Ios_Objective C_Uiview_Autolayout - Fatal编程技术网

Ios 使用带有.xib文件和自动布局的自定义UIView调整UIImageView的大小

Ios 使用带有.xib文件和自动布局的自定义UIView调整UIImageView的大小,ios,objective-c,uiview,autolayout,Ios,Objective C,Uiview,Autolayout,情况是这样的:我有一个VC,里面有一个自定义的UIView。此自定义UIView有一个.xib文件,其中包含2个UIImageView。我试图通过使用AutoLayout使这两个UIImageView在不同的屏幕大小上缩放到相同的大小。但是,当我启动应用程序时,自定义UIView中的图像超出了自定义UIView的超级视图(即VC)容器大小的范围。请帮忙。这里有一些图片可以更详细地解释这个问题 自定义UIView中的我的代码: @实现CustomTwoImageSelectedUIView -(i

情况是这样的:我有一个VC,里面有一个自定义的UIView。此自定义UIView有一个.xib文件,其中包含2个UIImageView。我试图通过使用AutoLayout使这两个UIImageView在不同的屏幕大小上缩放到相同的大小。但是,当我启动应用程序时,自定义UIView中的图像超出了自定义UIView的超级视图(即VC)容器大小的范围。请帮忙。这里有一些图片可以更详细地解释这个问题

自定义UIView中的我的代码: @实现CustomTwoImageSelectedUIView

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
        if (self) {
            [self load];
        }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
        if (self) {
            [self load];
        }
    return self;
}

- (void)load {
    [[NSBundle mainBundle] loadNibNamed:@"CustomTwoImagesSelectedUIView" owner:self options:nil];
    self.lastImageView.layer.cornerRadius = 5.0f;
    self.lastImageView.clipsToBounds = YES;
    self.secondToLastImageView.layer.cornerRadius = 5.0f;
    self.secondToLastImageView.clipsToBounds = YES;   
    [self addSubview:self.twoImagesSelectedView];
}

您已经设置了ImageView的cliptobounds属性,但我想ImageView本身的边界设置不正确。您需要在ImageView的.xib文件中仔细设置布局约束。也。将CustomImageView添加到某个视图控制器或superview时。您还需要设置CustomImageView相对于其superview的布局约束。

我已尝试在自定义UIView中使用AutoLayout,但它也不能工作。我认为这是Xcode的一个bug,我终于找到了这篇文章:

在本文中,我发现它使用了autoresizingMask而不是AutoLayout。这是解决UI显示问题的好方法。我希望它能帮助你

- (void)load {
    [[NSBundle mainBundle] loadNibNamed:@"CustomTwoImagesSelectedUIView" owner:self options:nil];
    self.lastImageView.layer.cornerRadius = 5.0f;
    self.lastImageView.clipsToBounds = YES;
    self.secondToLastImageView.layer.cornerRadius = 5.0f;
    self.secondToLastImageView.clipsToBounds = YES;   
    [self addSubview:self.twoImagesSelectedView];

    self.twoImagesSelectedView.frame = self.bounds;
    self.twoImagesSelectedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

我解决了这个问题,去掉了xib文件,并处理了main.storyboard上的自动布局约束。我很感激你的回答。