在我的應用程序我有一個嵌入式子視圖控制器到其在運行時我不得不根據數據來自該服務器中添加一個或多個「編輯UIView的」 S。我可以將每個「編輯器」建模爲xib文件中的完整UIViewController,並在運行時添加它。 「編輯」的父母都有自己的一些項目(如標題和幾個按鈕),所以我一直試圖建立使用一個孩子的UIViewController並載入「編輯」到其在飛行中的UIScrollView分層的方法。但是我不能讓Autolay合作。該圖顯示了基本安排。 自動佈局和的UIScrollView和子視圖控制器發出
問題始於剛剛在UIScrollview中嵌入「內容視圖」。我可以通過scrollview使內容視圖正常工作。當我添加編輯器視圖,並添加運行時間約束的內容來看,我得到多個自動佈局投訴加上滾動視圖內容大小爲0,0,所以我顯然沒有得到我所需要的。
關於如何解決這個問題的任何想法?我總是可以簡單地爲每個編輯器複製子控制器的額外項目,但以這種分層方式進行此項工作會很好。
注意我這樣做是先用一個UITableViewController每個編輯器是一個UITableViewCell,喜歡的是汽車的佈局,但我想看看,如果我能做到這一點不表。也許我會回到那個。
添加了這些在運行時:
_editor = [self editorForDataType];
[_contentView addSubview:_editor];
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:_editor
attribute:NSLayoutAttributeLeading
relatedBy:0
toItem:self.contentView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0];
[self.contentView addConstraint:leftConstraint];
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:_editor
attribute:NSLayoutAttributeTrailing
relatedBy:0
toItem:self.contentView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0];
[self.contentView addConstraint:rightConstraint];
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:_editor
attribute:NSLayoutAttributeLeading
relatedBy:0
toItem:self.contentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
[self.contentView addConstraint:topConstraint];
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:_editor
attribute:NSLayoutAttributeTrailing
relatedBy:0
toItem:self.contentView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];
[self.contentView addConstraint:bottomConstraint];
你有沒有設置translatesAutoresizingMaskIntoConstraints爲NO您_EDITOR看法? – rdelmar
是的,儘管默認情況下它似乎沒有。 – ahwulf
你用什麼方法添加約束? 'viewDidLoad'? –