我正在使用以下方法向 UINavigationBar 添加一系列按钮:
NSArray *items;
items = [NSArray arrayWithObjects:
fixedSpace,
refreshStopBarButtonItem,
flexibleSpace,
self.backBarButtonItem,
flexibleSpace,
self.forwardBarButtonItem,
flexibleSpace,
self.actionBarButtonItem,
fixedSpace,
nil];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, toolbarWidth, 44.0f)];
toolbar.items = items;
toolbar.tintColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
一切运作良好。
但是,当我旋转到横向模式时,uinavigationbar 中的工具栏不会旋转。
添加此代码(在 SO 上找到)会导致工具栏调整大小,但不会调整其中的按钮,因此它们在底部被部分裁剪,不再与工具栏背景对齐
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
CGRect navigationToolbarFrame = self.navigationController.navigationBar.frame;
CGRect customToolbarFrame = CGRectOffset(navigationToolbarFrame, 0.0, navigationToolbarFrame.size.height);
[UIView animateWithDuration:duration animations:^{
//self.toolbar.frame = customToolbarFrame;
// FAILS!!!
//self.navigationItem.rightBarButtonItem.toolbar.frame = customToolbarFrame;
// FAILS!!!
}];
}
在 uinavigationbar 中处理工具栏的正确方法是什么?就像是...
self.toolbar.frame = customToolbarFrame;
还是我必须为 UIBarButtonItems 指定自动调整大小掩码?...
self.backBarButtonItem.autoresizingMask = UIViewAutoresizingFlexibleHeight;
...尝试这样做失败了