我向导航栏添加了一个 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;
部分截图如下:
从纵向模式加载。
旋转到横向模式。
旋转回纵向模式。
如有任何建议,我们将不胜感激。
最佳答案
不要在导航栏中使用带边框的样式!使用 UISegmentedControlStyleBar
。这就是它的用途。
此外,不要设置分段控件的大小(框架)。只需使用 alloc-init
创建它,然后允许它使用自己的固有大小调整规则 (sizeToFit
)。
关于iphone - 从纵向旋转到横向时如何更改 UIBarButtonItem 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16475316/