Reputation: 14403
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:
loaded from portrait mode.
rotate to landscape mode.
rotate back to portrait mode.
Any suggestions will be appreciated.
Upvotes: 1
Views: 510
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