Using IB to add a UISegmentedControl to a NavigationBar

假装没事ソ 提交于 2019-12-13 14:27:17

问题


I'm current creating a UISegmentedControl programmatically in a view controller's viewDidLoad method and adding it to the view controller's navigation bar by assigning it to self.navigationItem.titleView.

That's easy enough, but I'd like to be able to do this in Interface Builder as well and so far haven't been able to figure out how. Google hasn't been much help either. Can someone describe how to do this in IB or point to an online example? I'd be much appreciative. Thanks, Howard


回答1:


If you've got whole nav stack in the nib, it's actually pretty easy; you can just drag it into the title area and IB does the right thing automatically.

Otherwise, you can just add the segmented control to the nib (not necessarily a subview) and then add an @property IBOutlet to it from your view controller. Then in viewDidLoad, assign it to the titleView as normal. Remember to release in dealloc, and you're golden.




回答2:


In IB you certainly can just drag a view into the middle of the navigation controller and it will work fine if its just inside one navigation item.

However, if the same view object reference is dragged into the title view area of different navigation items that will at some point be pushed onto the navigation controllers stack, you will run into problems with the title view disappearing when you travel back through the stack. The navigation controller isn't too happy with references to the same object popping up on multiple navigation items for some reason and it only throws a fit when you pop back to the view with the troublesome navigation item.

To get around this you MUST explicitly set and unset the titleView object when the you navigate to the views using the shared title view object reference. For instance, if you had custom logic behind a subclassed view set as the titleView that you only wanted to instantiate once.




回答3:


Alternatively, you could store the UISegmentedControl designed in IB in it's own NIB. Then set the FileOwner to the viewcontroller class that will be using the segmentedControl instance. In the viewcontroller class, declare the segmentedcontrol as an IBOutlet Property and link it to the instance in the nib.

All left to using the designed instance is then to call:

[[NSBundle mainBundle] loadNibNamed:@"TTCustomSegmentedControl"
                              owner:self
                            options:nil];
self.navigationItem.titleView = sortSegmentControl;    



回答4:


Just try this (works for me):

UISegmentedControl *mSegmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:
                                             @"Segment 1",
                                             @"Segment 2",
                                             nil]];

mSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
mSegmentedControl.tintColor = [UIColor redColor];

[mSegmentedControl setSelectedSegmentIndex:0];


[mSegmentedControl addTarget:self action:@selector(sectionPress:) 
           forControlEvents:UIControlEventValueChanged];

self.navigationItem.titleView = mSegmentedControl;



回答5:


You can't set the titleView property in IB, but you may be able to create / set up the control as a child of your controller's view via Interface Builder, and then in your viewDidLoad method, remove it from your view and set it as the titleView:

[segControl removeFromSuperview];
self.navigationItem.titleView = segControl;


来源:https://stackoverflow.com/questions/1150831/using-ib-to-add-a-uisegmentedcontrol-to-a-navigationbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!