【问题标题】:Resizing UIImageViews With Custom UIView with .xib File and AutoLayout使用带有 .xib 文件和 AutoLayout 的自定义 UIView 调整 UIImageViews 的大小
【发布时间】:2015-05-28 14:23:47
【问题描述】:

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

我在自定义 UIView 中的代码: @implementation CustomTwoImagesSelectedUIView

-(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];
}

【问题讨论】:

    标签: ios objective-c uiview autolayout


    【解决方案1】:

    您已经设置了 imageviews 的 cliptobounds 属性,但我猜 imageviews 本身的边界设置不正确。您需要在 .xib 文件中为图像视图仔细设置布局约束。还。当您将 customImageViews 添加到某个视图控制器或超级视图时。您还需要设置 customImageViews 相对于其父视图的布局约束。

    【讨论】:

    • 我通过删除 xib 文件解决了这个问题,并处理了 main.storyboard 上的自动布局约束。感谢您的回答。
    【解决方案2】:

    我在自定义 UIView 中尝试了 AutoLayout,但它也无法正常工作。我认为是Xcode的一个bug,终于找到了这篇文章:Create an IBDesignable UIView subclass with code from an XIB file in Xcode 6

    在文章中,我发现它使用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-31
      • 2018-07-09
      • 2015-05-26
      • 2013-10-21
      • 2014-12-01
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      相关资源
      最近更新 更多