一、viewController的屬性edgesForExtendedLayout
如果我們使用masonry晃听,這個屬性的設(shè)置會非常方便的幫助我們解決計算偏移量的問題铐维。
1.edgesForExtendedLayout是一個類型為UIExtendedEdge的屬性懈词,指定邊緣要延伸的方向楔绞。默認是UIRectEdgeAll结闸,四周邊緣均延伸,從iOS7開始
如果即使視圖中上有navigationBar酒朵,下有tabBar桦锄,那么視圖仍會延伸覆蓋到四周的區(qū)域。
2.假設(shè)在有navigation bar和tabbar的情況下:
該屬性設(shè)置為UIRectEdgeBottom;那么就會self.view.frame是從navigationBar下面開始計算一直到屏幕底部蔫耽;
該屬性設(shè)置為UIRectEdgeTop;那么就會self.view.frame是從navigationBar上面計算面開始計算一直到屏幕tabBar上部结耀;
該屬性設(shè)置為UIRectEdgeNone;那么就會self.view.frame是從navigationBar下面開始計算一直到屏幕tabBar上部;
3.iOS7以上系統(tǒng),self.navigationController.navigationBar.translucent默認為YES图甜,self.view.frame.origin.y從0開始(屏幕最上部)碍粥。此時若是添加代碼self.edgesForExtendedLayout = UIRectEdgeNone(iOS7.0以后方法);self.view.frame.origin.y會下移64像素至navBar下方開始。
#define kIOSVersion ((float)[[[UIDevice currentDevice] systemVersion] doubleValue])
在viewController中使用
if (kIOSVersion >= 7.0) {
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;
}
二黑毅、multipliedBy()嚼摩、dividedBy()比例
使用multipliedBy必須是對同一個控件本身,比如博肋,上面的代碼中低斋,我們都是對bottomInnerView.mas_width本身的蜂厅,如果修改成相對于其它控件匪凡,會出問題。
dividedBy(3);可以不是
三掘猿、AutoLayout關(guān)于更新的幾個方法的區(qū)別
setNeedsLayout:告知頁面需要更新病游,但是不會立刻開始更新。執(zhí)行后會立刻調(diào)用layoutSubviews稠通。
layoutIfNeeded:告知頁面布局立刻更新衬衬。所以一般都會和setNeedsLayout一起使用。如果希望立刻生成新的frame需要調(diào)用此方法改橘,利用這點一般布局動畫可以在更新布局后直接使用這個方法讓動畫生效滋尉。
layoutSubviews:系統(tǒng)重寫布局
setNeedsUpdateConstraints:告知需要更新約束,但是不會立刻開始
updateConstraintsIfNeeded:告知立刻更新約束
updateConstraints:系統(tǒng)更新約束
四飞主、Masonry使用注意事項
用mas_makeConstraints的那個view需要在addSubview之后才能用這個方法
mas_equalTo適用數(shù)值元素狮惜,equalTo適合多屬性的比如make.left.and.right.equalTo(self.view)
方法and和with只是為了可讀性,返回自身碌识,比如make.left.and.right.equalTo(self.view)和make.left.right.equalTo(self.view)是一樣的碾篡。
因為iOS中原點在左上角所以注意使用offset時注意right和bottom用負數(shù)。
五筏餐、重寫updateViewConstraints方法
- (void)updateViewConstraints
ViewController的View在更新視圖布局時开泽,會先調(diào)用ViewController的updateViewConstraints 方法。我們可以通過重寫這個方法去更新當前View的內(nèi)部布局魁瞪,而不用再繼承這個View去重寫-updateConstraints方法穆律。我們在重寫這個方法時,務(wù)必要調(diào)用 super 或者 調(diào)用當前View的 -updateConstraints 方法导俘。
#import "ZYXSixViewController.h"
@interface ZYXSixViewController ()
@property (nonatomic, strong) UIButton * yellowButton;
@property (nonatomic, assign) BOOL buttonClicked;
@end
@implementation ZYXSixViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self p_setupViews];
}
- (void)p_setupViews
{
self.buttonClicked = NO;
//實例1
UIButton * yellowButton = [UIButton buttonWithType:UIButtonTypeCustom];
yellowButton.backgroundColor = [UIColor yellowColor];
[yellowButton setTitle:@"點擊尺寸改變" forState:UIControlStateNormal];
[yellowButton setTitleColor:[UIColor blackColor] forState:
UIControlStateNormal];
yellowButton.layer.cornerRadius = 5;
[self.view addSubview:yellowButton];
self.yellowButton = yellowButton;
[yellowButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100, 100));
make.center.equalTo(self.view);
}];
[yellowButton addTarget:self action:@selector(yellowButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)yellowButtonClicked:(UIButton *)sender{
self.buttonClicked = !self.buttonClicked;
// 告訴self.view約束需要更新
[self.view setNeedsUpdateConstraints];
// 調(diào)用此方法告訴self.view檢測是否需要更新約束众旗,若需要則更新,下面添加動畫效果才起作用
[self.view updateConstraintsIfNeeded];
[UIView animateWithDuration:1 animations:^{
// 告知頁面布局立刻更新
[self.view layoutIfNeeded];
}];
}
- (void)updateViewConstraints
{
// 這里使用update也是一樣的趟畏。
// remake會將之前的全部移除贡歧,然后重新添加
__weak __typeof(self) weakSelf = self;
[self.yellowButton mas_remakeConstraints:^(MASConstraintMaker *make) {
if (weakSelf.buttonClicked) {
make.top.bottom.mas_equalTo(0);
make.left.right.mas_equalTo(0);
}else{
make.size.mas_equalTo(CGSizeMake(100, 100));
make.center.equalTo(self.view);
}
}];
[super updateViewConstraints];
}