Saturday, December 7, 2013

How to add Navigation SegmentedControl middle (with code)

I found online is added to the left and right buttons, as follows:
UISegmentedControl * segmentedControl = [[UISegmentedControl alloc] initWithFrame: CGRectMake (80.0f, 8.0f, 150.0f, 30.0f)];
[SegmentedControl insertSegmentWithTitle: @ "One" atIndex: 0 animated: YES];
[SegmentedControl insertSegmentWithTitle: @ "Two" atIndex: 1 animated: YES];
[SegmentedControl insertSegmentWithTitle: @ "Three" atIndex: 2 animated: YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
segmentedControl.multipleTouchEnabled = NO;
[SegmentedControl addTarget: self action: @ selector (SelectSeg) forControlEvents: UIControlEventValueChanged];
UIBarButtonItem * segButton = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
self.seg = segmentedControl;
self.seg.selectedSegmentIndex = 0;
[SegmentedControl release];
self.navigationItem.rightBarButtonItem = segButton;
[SegButton release];

But now the situation is left button is used as the back button, right button also has another use, so take this SegmentControl on navigation in the middle, I ask how to write
Reply:
1 UINavigationbar not have a property is titleView, titleView is the location of the center;
2 The UISegmentedControl is based on the UIView;
3 In this case, you know. .
Reply:
cited a floor reply:
1 UINavigationbar not have a property is titleView, titleView middle position;
2 The UISegmentedControl is based on the UIView;
3 In this case, you know. .


You say is that right
segmentedControl = [[UISegmentedControl alloc] initWithFrame: CGRectMake (80.0f, 8.0f, 150.0f, 30.0f)];
[SegmentedControl insertSegmentWithTitle: @ "One" atIndex: 0 animated: YES];
[SegmentedControl insertSegmentWithTitle: @ "Two" atIndex: 1 animated: YES];
[SegmentedControl insertSegmentWithTitle: @ "Three" atIndex: 2 animated: YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
segmentedControl.multipleTouchEnabled = NO;
[SegmentedControl addTarget: self action: @ selector (SelectSeg) forControlEvents: UIControlEventValueChanged];
/ / UIBarButtonItem * segButton = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
self.seg = segmentedControl;
self.seg.selectedSegmentIndex = 0;
[Self.navigationItem.titleView addSubview: segmentedControl];


Seems not, do not show up

Reply:
Not addsubview, is [self.navigationItem setTitleView: segmentedControl];
You write.
Reply:
reference to the third floor of the reply:
not addsubview, is [self.navigationItem setTitleView: segmentedControl];
You write.


Can, thank you very much.

No comments:

Post a Comment