UINavigationItem titleView定位问题

时间:2013-06-28 09:15:12

标签: ios uinavigationitem

我有一个iPad应用程序,在其中一个屏幕上,我将UIToolbar设置为titleView viewController的{​​{1}}。我还有一个navigationItem和一个left-

当我以横向方式进入屏幕并旋转设备时,rightBarButtonItem仍然居中。但是,如果我执行相反操作(以纵向输入并旋转设备),titleView将向右移动。有没有什么办法解决这一问题?这是我的代码:

titleView

修改

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; 在两种情况下都是相同的,更改的内容是self.navigationItem.titleView.frame.size

1 个答案:

答案 0 :(得分:3)

如果您使用工具栏,请不要将titleView添加为subView。而是添加项目数组。

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

将栏按钮项添加为titleViewBtn并使边框变为纯白。

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