导航栏标题视图定位问题

4
我有一个iPad应用程序,在其中一个屏幕上,我有一个UIToolbar设置为viewControllernavigationItemtitleView。我还有一个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 的大小在两种情况下都相同,改变的是 origin.x


尝试使用 sizeToFit - Bhavin
在您旋转设备时,更改导航控制器的位置 :) - user1525369
1个回答

3

如果您正在使用工具栏,请不要将titleView添加为子视图。而是将其添加到items数组中。

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接