4

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

4

1 回答 1

3

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

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;
于 2013-07-01T09:17:10.250 回答