I'm trying to recreate (to some extent) the up and down arrows presented on Mail.app on iOS 7 and iOS 8. There's not much to it. I just insert a UISegmentedControl
into a UIBarButtonItem
, like this (if you do know a better way of replicating that UI, please let me know):
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[[UIImage imageNamed:@"ArrowDown"], [UIImage imageNamed:@"ArrowUp"]]];
[segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
[segmentedControl setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedControl setDividerImage:[[UIImage alloc] init] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedControl setFrame:CGRectMake(0, 0, 100, 30)];
[segmentedControl setMomentary:YES];
UIBarButtonItem *segmentedButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = segmentedButton;
Notice the calls to setBackgroundImage:
and setDividerImage:
. They are needed so the control looks just like the one from Mail.app. And that setup works:
Trouble is, it doesn't stay that way after a rotation. When using the iPhone in landscape, the rightBarButtonItem
is automatically resized, looking broken and staying that way even after returning the device to portrait mode:
The control still works as planned , but well... It doesn't look so pretty anymore.
I have experimented with lots of properties and different setups, and nothing seems to work, except not setting an empty UIImage for the control background. If I comment out the setBackgroundImage:
and the setDividerImage:
pieces of code, the control keeps its look even after rotating:
But that's not the UI I have been looking for.
Do you know of any way to achieve the intended look and still have the correct behavior around? Or even what could be causing the resizing in the first place?