2013-05-10 101 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

相關問題