基於 xib 開發自適應高度 view


基於 xib 開發自適應高度 view

雖然 xib 和 storyboard 不那么受待見,但我依舊喜歡。


自定義 view

說明:
title Label 約束 -> top:0 bottom:0 left:15
indicator Label 約束 -> top:5 bottom:5 right:34
title Label 和 indicator Label 相對約束 -> 水平間距 >= 5

indicator Label 設置 -> numberOfLines = 0

效果
筆記

使用自適應,也就是不確定約束,多個自適應控件之間不應該出現確定的約束(明確規定 rect 的約束)。
比如: 上面 title Label 和 indicator Label 都使用了自適應(title Label 沒有確定寬度或者右邊距,indicator Label 沒有確定寬度或者左邊距),如果這時設置這兩個 label 的水平間距為一個確定的值(兩個 label 在 xib 上的當前間距除外),約束就會自相矛盾。

核心代碼
// 初始化
- (CHDisclosureIndicatorView *) diView {
	if (_diView == nil) {
    	_diView = [[[UINib nibWithNibName:@"CHDisclosureIndicatorView" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];
    	_diView.layer.cornerRadius = 5;
    	_diView.delegate = self;
    	_diView.title = @"默默";
    	_diView.indicator = nil;
	}
	return _diView;
}    

// 設置約束
[self.diView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.topView.mas_bottom).offset(10);
    make.left.mas_equalTo(15);
    make.right.mas_equalTo(-15);
    make.height.mas_greaterThanOrEqualTo(50); // important
}];    
完整 demo

demo


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM