I have added a custom button to the navigation bar's custom right bar button item as shown in the image.
I'm want to be able to remove the space after the button so that it touches the right edge of the screen but couldn't find a solution even after searching. If someone could mention how, would be great.
This is the code I'm using
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(50.0, 0.0, 60, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(testButtonControl:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = aBarButtonItem;
You can add a fixed space element with a negative width (I know, this sounds like a hack, but it works):
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(50.0, 0.0, 60, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(testButtonControl:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *spaceFix = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
spaceFix.width = -5;
self.navigationItem.rightBarButtonItems = @[spaceFix, aBarButtonItem];
You have to create a new view, add button and everything else you want and add it to the titleView as shown.
- (void)viewDidLoad {
self.navigationItem.titleView = [self titleView];
}
- (UIView *)titleView {
CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat width = 0.95 * self.view.frame.size.width;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, navBarHeight)];
..
// please add what you need here
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With