2015-03-25 78 views
0

以下是這種情況:我有一個VC裏面有一個Custom UIView。這個自定義UIView有一個.xib文件,其中包含2個UIImageViews。我試圖通過使用AutoLayout讓這兩個UIImageView在不同的屏幕尺寸上縮放到相同的尺寸。然而,當我啓動我的應用程序時,我的Custom UIView中的圖像超出了Custom UIView的超級視圖(這是VC)容器大小的界限。請幫忙。這裏有一些圖片可以更詳細地解釋問題。使用.xib文件和AutoLayout自定義UIView調整UIImageViews的尺寸

我的代碼在自定義的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]; 
} 

回答

0

您已設置imageviews的cliptobounds屬性,但綁定imageviews本身設置不正確我猜的。您需要在image.xml的.xib文件中仔細設置佈局約束。也。將customImageViews添加到某個視圖控制器或超級視圖時。您還需要針對其超級視圖設置customImageView的佈局約束。

+0

我通過擺脫xib文件解決了這個問題,並關注了main.storyboard上的自動佈局約束。我很欣賞答案。 – ddeger 2015-03-26 03:33:21

0

我已經嘗試在自定義UIView中的AutoLayout,但它不能工作。我認爲這是一個來自Xcode的錯誤,我終於找到這篇文章:Create an IBDesignable UIView subclass with code from an XIB file in Xcode 6

在文章中,我發現它使用autoresizingMask而不是AutoLayout。這是解決用戶界面顯示問題的好方法。我希望它能幫助你。

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