2013-05-10 48 views
1

我在导航栏中添加一个UISegmentedControl。当纵向加载视图时,其框架看起来是正确的。但是当它旋转到横向时,UIBarButtonItem变大。如果再次旋转到肖像,它仍然保持较大的一个。如何在从纵向旋转到横向时更改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

任何建议将不胜感激。

+0

使用autoresizingmask。请参考这里[UIView autoresizingmask](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html) – Ashini 2013-05-10 04:49:47

+0

@Ashini我试过autoresingmask,它的工作更糟糕:(。 – chancyWu 2013-05-10 05:17:45

回答

1

请勿在导航栏中使用边框样式!使用UISegmentedControlStyleBar。这就是它的目的。

此外,不要设置分段控件的大小(帧)。只需用alloc-init创建它,然后允许它使用自己的内在尺寸规则(sizeToFit)。

+0

感谢您的回答。 – chancyWu 2013-05-10 05:42:07

相关问题