UIButton style when added to navigationItem.titleView

I need to add a button in the UINavigationItem titleView (actually in the center of the UINavigation panel).

I used the following code for this:

 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(0, 0, 70, 30); [btn setSelected:YES]; [btn setTitle:@"list" forState:UIControlStateNormal]; 

And I move on to the following:

enter image description here

Do I need to point the same style of the Finish button (which is UIBarButtonItem ) to the List button?

+3
source share
4 answers

You can use UISegmentedControl:

 UISegmentedControl *mySegmentedControl = [[UISegmentedControl alloc] initWithItems:@"Example"]]; mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; [mySegmentedControl addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventValueChanged]; self.navigationItem.titleView = mySegmentedControl; 
+7
source

UIBarButtonItems can only be used as elements in a UIToolbar (they are not actually UIView instances), so the only way to directly use the UIBarButtonItem in your titleView would be to set the UIToolbar instance as a titleView and add its barButtonItem to this toolbar. However, I do not think this will work.

I think you will need to find a way to simulate the UIBarButtonItem style using the plain old UIButton. You can get the actual image file used to create the UIBarButtonItems using the UIKit Wallpaper Browser . Then it's just a matter of creating a custom UIButton using this background image, and you're good to go.

+1
source

I used the andrej UISegmentedControl-based approach and expanded it a bit to behave like a regular UIButton:

 @interface SPWKBarButton : UISegmentedControl - (id)initWithTitle:(NSString *)title; - (void)setTitle:(NSString *)title; @end @implementation SPWKBarButton - (id)initWithTitle:(NSString *)title { self = [super initWithItems:@[title]]; if (self) { self.segmentedControlStyle = UISegmentedControlStyleBar; NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; [self setTitleTextAttributes:attributes forState:UIControlStateNormal]; } return self; } - (void)setTitle:(NSString *)title { [self setTitle:title forSegmentAtIndex:0]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; if (CGRectContainsPoint(self.bounds, [[touches anyObject] locationInView:self])) { self.selectedSegmentIndex = 0; } else { self.selectedSegmentIndex = UISegmentedControlNoSegment; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; if (CGRectContainsPoint(self.bounds, [[touches anyObject] locationInView:self])) { [self sendActionsForControlEvents:UIControlEventTouchUpInside]; } self.selectedSegmentIndex = UISegmentedControlNoSegment; } @end 

For basic use, you can use it as a replacement using UIButton:

 _button = [[SPWKBarButton alloc] initWithTitle:titles[0]]; [_button addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside]; [self.navigationItem setTitleView:_button]; 

The event will fire only if the touch occurs within the borders of the segmented control. Enjoy it.

+1
source

Yes, you should use UIBarButtonItem too for the list button

using a custom identifier and header properties.

You can use the UIBarButtonItem flex space identifier to display the list button in the center.

0
source

All Articles