chancyWu
chancyWu

Reputation: 14403

how to change the UIBarButtonItem size when rotate from the portrait to landscape?

I add one UISegmentedControl to the navigation bar. When the view loaded in portrait, its frame seems right. but when it rotate to landscape, the UIBarButtonItem becomes larger. If rotate to portrait again, it will still remain the larger one.

Some code snippet is here:

#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;

some screenshots are as follows:

  1. loaded from portrait mode. enter image description here

  2. rotate to landscape mode. enter image description here

  3. rotate back to portrait mode. enter image description here

Any suggestions will be appreciated.

Upvotes: 1

Views: 510

Answers (1)

matt
matt

Reputation: 535576

Do not use the bordered style in a navigation bar! Use UISegmentedControlStyleBar. That's what it's for.

Also, do not set the size (frame) of the segmented control. Just create it with alloc-init and then allow it to use its own intrinsic sizing rules (sizeToFit).

Upvotes: 1

Related Questions