iphone - 从纵向旋转到横向时如何更改 UIBarButtonItem 大小?

标签 iphone ios uiview uinavigationcontroller uibarbuttonitem

我向导航栏添加了一个 UISegmentedControl。当 View 以纵向加载时,它的框架看起来是正确的。但是当它旋转到横向时,UIBarButtonItem 会变大。如果再次旋转到纵向,它仍然是较大的那个。

一些代码片段在这里:

#define SEGMENT_WIDTH   100.0
#define SEGMENT_HEIGHT  32.0

CGRect segmentedControlRect = CGRectMake(0, 0, SEGMENT_WIDTH, SEGMENT_HEIGHT);
segmentedControl = [[UISegmentedControl alloc] initWithFrame:segmentedControlRect];
segmentedControl.momentary = NO;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
[segmentedControl addTarget:self action:@selector(tabButtonPressed:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segmentBarBtn = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

self.navigationItem.rightBarButtonItem = segmentBarBtn;

部分截图如下:

  1. 从纵向模式加载。 enter image description here

  2. 旋转到横向模式。 enter image description here

  3. 旋转回纵向模式。 enter image description here

如有任何建议,我们将不胜感激。

最佳答案

不要在导航栏中使用带边框的样式!使用 UISegmentedControlStyleBar。这就是它的用途。

此外,不要设置分段控件的大小(框架)。只需使用 alloc-init 创建它,然后允许它使用自己的固有大小调整规则 (sizeToFit)。

关于iphone - 从纵向旋转到横向时如何更改 UIBarButtonItem 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16475316/

相关文章:

ios - 解析查询检索指针列

ios - NSURLErrorDomain Code=-2000 "can’ t 从网络加载”

ios - 充满 plist 信息的 Detail View

ios - 重叠透明 UIView

iphone - TableView 应用因 'NSInternalInconsistencyException' 而终止

iphone - 将 subview 显示为弹出窗口时出现问题

iphone - Core Data 或 Sqlite 与 FMDB 用于基于 RSS 提要的应用程序?

iphone - 在 iphone 的谷歌地图上绘制路线

ios - Jetsam 的真正作用是什么?

ios - 将对象设置为 .hidden = true 和设置 .alpha = 0 有什么区别?