2

As below, I set NavigationBar Height without using NavigationController.
(use navigation bar as a standalone object)

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];

But it doesn’t work on iOS11 beta.
Actually, the NavBar Height looks always default Height = 44.
It works before iOS10.

I tried beta7, but nothing changes.
Is there any way to set NavigationBar Height?

addition
I tried to apply Autolayout Constraint like following code.
In this case, "leadingAnchor", "topAnchor", "widthAncohr" works as I expected.
but I couldn't control "heightAnchor".
This is tried on iOS11 GM version.

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]]];
ntmy
  • 21
  • 2
  • can you add some additional code – Anbu.Karthik Aug 23 '17 at 05:26
  • sorry, I added. but it's just simple sample code. – ntmy Aug 23 '17 at 05:28
  • The navigationBar uses autolayout to layout the subviews. Try adding a constraint with for the height anchor like: '[NSLayoutConstraint activateConstraints:@[[[navBar heightAnchor] constraintEqualToConstant:40.0]]];' – piyuj Aug 31 '17 at 14:35
  • I have a similar issue. Any news on this? – Kai Sep 08 '17 at 12:54
  • @Kai I edit additional part on my post. I've not found the way to resolve this problem... – ntmy Sep 19 '17 at 02:22
  • @ntmy I gave up on trying to use a standalone navigation bar. Instead I am now always presenting my view controllers in navigation controller which just works fine. I can modify the navigation bar on the navigation controller just like I want to. – Kai Sep 19 '17 at 08:43
  • @Kai I see. If there is restriction to use standalone navigation, this answer at forum may be useful. https://forums.developer.apple.com/message/259219#259219 – ntmy Sep 20 '17 at 00:21
  • Your code is fine it's an issue with xcode9. Try to build it with xcode8.3.3 and it will look normal as expected. – Faisal Sep 23 '17 at 10:13

0 Answers0