我想用Masonry在导航栏20px下面画一个按钮,所以我使用下面的代码
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).with.offset(20);
make.right.equalTo(self.view.mas_right);
make.top.equalTo(@(self.topLayoutGuide.length + 20));
make.height.equalTo(@30);
}];
但它不起作用!除非我像这样更改代码
make.top.equalTo(@84);
所以我不想使用@ 84,还有其他方法吗?谢谢!!
答案 0 :(得分:0)
尝试
make.top.equalTo(@(self.navigationController.navigationBar.frame.size.height + 20));
答案 1 :(得分:0)
最好使用navigationBar和statusBarFrame属性来获得最高偏移量。
您可以使用以下内容:
CGFloat topbarHeight = ([UIApplication sharedApplication].statusBarFrame.size.height +
(self.navigationController.navigationBar.frame.size.height ? : 0.0));
// button
[button mas_makeConstraints:^(MASConstraintMaker* make) {
make.height.equalTo(@30);
make.top.equalTo(self.view.mas_top).with.offset(topbarHeight);
make.left.equalTo(self.view.mas_left).with.offset(20);
make.right.equalTo(self.view.mas_right);
}];
但最好使用UIKit的layoutGuides:
[topView mas_makeConstraints:^(MASConstraintMaker *make) {
UIView *topLayoutGuide = (id)self.topLayoutGuide;
make.top.equalTo(topLayoutGuide.bottom);
make.left.equalTo(self.view);
make.right.equalTo(self.view);
make.height.equalTo(@100);
}];
<强>已更新强>
但现在已经弃用了:) iOS 11.0及更高版本我们可以使用UIView的safeAreaLayoutGuide属性。
if (@available(iOS 11.0, *)) {
make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
} else {
UIView *topLayoutGuide = (id)self.topLayoutGuide;
make.top.equalTo(topLayoutGuide.mas_bottom);
}