Using IB to add a UISegmentedControl to the navigation bar

I am currently creating it UISegmentedControlprogrammatically as a controller viewDidLoadand adding it to the navigation pane of the view controller, assigning it self.navigationItem.titleView.

It's simple enough, but I would like to be able to do this in Interface Builder, and still have not been able to figure out how to do this. Google didn't help either. Can someone describe how to do this in IB or provide an online example? I would be very grateful. Thanks, Howard

+3
source share
5 answers

If you have the whole stacker in the bar, this is actually quite easy; you can just drag it to the header area and IB will do it right.

Otherwise, you can simply add a segmented control to nib (not necessarily a subview), and then add a view @property IBOutletto it from your controller. Then in viewDidLoad, assign its titleView as usual. Remember to release at dealloc and you are golden.

+6
source

In IB, you can of course just drag the view into the middle of the navigation controller, and it will work fine if it is inside the same navigation element.

, , - , , . - , , , .

, titleView, , . , , titleView, .

+1

UISegmentedControl, IB, NIB. FileOwner viewcontroller, segmentedControl. viewcontroller segmentedcontrol IBOutlet nib.

, , :

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

Just try (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;
+1
source

You cannot set the property titleViewin IB, but you can create / configure the control as a child of your controller view through Interface Builder, and then in your method viewDidLoad, remove it from your view and set it as titleView:

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

Source: https://habr.com/ru/post/1713005/


All Articles