簡體   English   中英

以編程方式在子類UIView的子視圖上進行Autolayout

[英]Autolayout programmatically on a subview of a subclassed UIView

我已經將UIView子類化為一個自定義的groupView,我用它來以簡單的方式為我的布局添加一些東西。 這個groupView包含一個用作標題的UILabel和一個用它在背景顏色的CALayer上繪制roundRect的UIView。 想想UITableView的部分。 我通過刪除UIView並將其類更改為我的子類,將此groupView添加到storyboard中。 一切正常,我通過Xcode中的用戶定義的運行時屬性設置標題。 一切都很棒,我在故事板上將UILabels添加到此視圖中,並在運行時創建標題標簽並進行圓整。

我的groupView結構:

  1. groupView:(UIView)clipToBounds:NO;
    • 標題:(UILabel)位於groupView上方。
    • contentView:(UIView)通過CALayer創建roundRect和color,應該與groupView的大小相同。

所以有什么問題? 好吧,處理autolayout是一個痛苦的開始,但為了使這個子類UIView工作,我需要以編程方式設置contentView約束。 我無法弄清楚這種自動布局ASCII格式字符串的語法。 目前我有:

 _contentView = [[UIView alloc]initWithFrame:self.bounds];

    _contentView.layer.cornerRadius = 5.0f;
    _contentView.layer.masksToBounds=YES;
    _contentView.backgroundColor=_backgroundColor;
    _contentView.layer.borderWidth=_borderWidth;
    _contentView.layer.borderColor=_borderColor.CGColor;
    [self insertSubview:_contentView atIndex:0];
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(self,_contentView);
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[self]-0-[_contentView]-0-[self]" options:0 metrics:nil views:viewsDictionary];

    for (NSLayoutConstraint *constraint in constraints) {
        [_contentView addConstraint:constraint];
    }

哪個崩潰: *由於未捕獲的異常'NSGenericException'終止應用程序,原因:'無法在視圖上安裝約束。 約束是否引用了視圖子樹之外的內容? 那是違法的。 約束:查看:>'

我先嘗試過這個但它仍然沒有用:

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_contentView);
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[_contentView]-0-|" options:0 metrics:nil views:viewsDictionary];

哪個崩潰: *由於未捕獲的異常'NSGenericException'終止應用程序,原因:'無法在視圖上安裝約束。 約束是否引用了視圖子樹之外的內容? 那是違法的。 約束:查看:>'

RANT:不知怎的,這個AutoLayout假設是為了節省我們的工作,但我不知道這些好處如何影響現在的開銷。 為什么他們從使用引用和方法,甚至鍵入defs到這個古老的格式字符串? 它會更容易:[_ contentView約束:NSLayoutFormatAlignLeading toView:self withDistance:0.0f]; 或類似的東西? 在這一點上,我更願意處理彈簧和支柱。

任何幫助理解,或向我展示將contentView約束到自我大小的語法將是有幫助的。

錯誤告訴您需要知道的內容:

約束是否引用了視圖子樹之外的內容?

是的,它確實。 它引用了它的超級視圖。

您正在制作的這些約束需要應用於groupView ,在本例中為groupView

| character代表可視格式語言的superview,所以我想你想要:

@"|-0-[_contentView]-0-|"

然后你需要將約束添加到self而不是contentView,因為你需要將它添加到一個可以看到所有參與者的視圖中。

以下是視覺語言的參考: https//developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/formatLanguage.html#//apple_ref/doc/uid/TP40010853-CH3- SW1

您還可以通過以下方式完全避免使用以下格式:

+(id)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c

嘗試檢查constrints,它們是否正確添加到視圖中

例如:

UIView *yellowView = [[UIView alloc] init];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:yellowView];

//ios 9 last
NSLayoutConstraint *heightAnchor = [yellowView.heightAnchor constraintEqualToAnchor:self.purView.heightAnchor];
// addConstraints
[self.view  addConstraints:@[heightAnchor]];
[self.view setNeedsLayout];

如果將heightAnchor to添加heightAnchor to yellowView就像跟隨。 它會顯示錯誤

[yellowView addConstraints:@ [heightAnchor]];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM