UINavigationItem titleView positioning problem

I have an iPad app, and on one of the screens I set UIToolbar as titleView viewController navigationItem . I also have left- and rightBarButtonItem .

When I enter the screen in landscape orientation and rotate the device, the titleView remains centered. However, if I do the opposite (enter a portrait and rotate the device), the titleView to the right. Is there any way to fix this? Here is my code:

 UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, self.navigationController.navigationBar.frame.size.height)]; UIToolbar *titleToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 350, self.navigationController.navigationBar.frame.size.height)]; titleToolbar.items = @[commentButton, spacer2, downloadButton, spacer3, homeButton, spacer4, pageDisplayButton, spacer5, searchButton]; titleToolbar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; [titleView addSubview:titleToolbar]; self.navigationItem.titleView = titleView; 

Edit:

In both scripts, self.navigationItem.titleView.frame.size the same as changing origin.x

+4
source share
1 answer

If you use a toolbar, do not add a titleView as a subview. Instead, add an array of elements.

 UIToolbar *titleToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 350, self.navigationController.navigationBar.frame.size.height)]; UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, titleToolbar.frame.size.height)]; 

Add a panel item as titleViewBtn and make the border simple.

 titleToolbar.items = @[commentButton, spacer2, downloadButton, spacer3, homeButton, spacer4, **titleViewBtn** , pageDisplayButton, spacer5, searchButton]; titleToolbar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
+3
source

Source: https://habr.com/ru/post/1488651/


All Articles