UISegmentedControl's alignment get change when hiding UINavigationItem buttons
I have an UISegmentedControl
in the title view of the navigation bar. Also i have right bar button(plus) and left bar button(Edit) as shown in the below image..
I'm hiding the two bar buttons (here, edit & plus buttons) when the ID segment is clicked. This will be like..
I'm using the following code to hide bar button items.
- (void)segmentClicked {
segment = segmentedControl.selectedSegmentIndex;
self.navigationItem.leftBarButtonItem = leftBarButton; //leftBarButton is an instance of UIBarButtonItem
self.navigationItem.leftBarButtonItem = rightBarButton; //rightBarButton is an instance of UIBarButtonItem
if (segment == 0) {
//Do something for segment 1 click
}
else if (segment == 1) {
//Do something for segment 2 click
}
else {
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.rightBarButtonItem = nil;
}
}
My problem is, when i switch from segment1(Department) or segment2(Name) to segment3(ID), the UISegmentedControl
alignment get开发者_StackOverflow中文版 changed. How can i make it stable?
Thanks in Advance
bar button items align automatically.. if you remove other items the segmented control will shift. What you need to do is use a 'fixed space' bar item to position your segmented control... instead of making your leftBarButtonItem nil, change it to a fixed space item of size required to keep the segmented control in place.. if you want to see how it works before implementing, just put a tool bar in a xib and add 'fixed space' and 'flexible space' items..
EDIT:
its pretty straight forward. Just look at the uibarbuttonitem class. create a system bar button item object of type fixed space and set it's width as per your need. and set it as your left bar button:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
item.width = 50;
self.navigationItem.leftBarButtonItem = item;
[item release];
EDIT 2:
another suggestion - use shorter text for the segments. like use Dept. instead of Department. if your segmented control in title view is short enough it wont be affected by bar buttons.
精彩评论