不使用NavigationController的NavigationBar高度在iOS11中

时间:2017-08-23 05:24:38

标签: ios uinavigationbar ios11

如下所示,我在不使用NavigationController的情况下设置了NavigationBar高度 (使用导航栏作为独立对象)

UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];

UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:@"title"];

UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
navItem.leftBarButtonItem = cancelBtn;

UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)];
navItem.rightBarButtonItem = doneBtn;

[navbar setItems:@[navItem]];
[self.view addSubview:navbar];

但它不适用于iOS11测试版 实际上,NavBar高度看起来总是默认高度= 44 它适用于iOS10之前。

我试过beta7,但没有任何改变 有没有办法设置NavigationBar高度?

除了
我试着像下面的代码那样应用Autolayout Constraint 在这种情况下," leadingAnchor"," topAnchor"," widthAncohr"像我预期的那样工作 但是我无法控制" heightAnchor" 这是在iOS11 GM版本上试用的。

UINavigationBar* navbar = [[UINavigationBar alloc] init];
navbar.translatesAutoresizingMaskIntoConstraints = false;

UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:@"title"];

UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
navItem.leftBarButtonItem = cancelBtn;

UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)];
navItem.rightBarButtonItem = doneBtn;

[navbar setItems:@[navItem]];
[self.view addSubview:navbar];

[NSLayoutConstraint activateConstraints:@[[[navbar leadingAnchor] constraintEqualToAnchor:self.view.leadingAnchor constant:0]]];
[NSLayoutConstraint activateConstraints:@[[[navbar topAnchor] constraintEqualToAnchor:self.view.topAnchor constant:50]]];
[NSLayoutConstraint activateConstraints:@[[[navbar heightAnchor] constraintEqualToConstant:100.0]]];
[NSLayoutConstraint activateConstraints:@[[[navbar widthAnchor] constraintEqualToAnchor:self.view.widthAnchor]]];

0 个答案:

没有答案