I have had a medical calculation app for cardiac ultrasound in the App Store since 2011. At the bottom of each data entry screen is a toolbar with buttons to analyze all data, designate the open calculation as a favorite, and clear all active data from all calculations. After the most recent iOS update to 13.4.1, the Clear button at the bottom right of the screen has become unresponsive. If I put the app in the background, open another app, and then switch back to my app, then the Clear button becomes responsive again. If I move the Clear button to the left side of the toolbar, next to the Analyze button, then it functions without a problem. Since the code that displays these buttons has been the same for years and multiple iOS versions, I am wondering if possibly there is a bug in iOS 13.4.1 causing this issue.
Here is the code used to generate the toolbar:
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight - [sharedVariableState g_toolbar_adjuster_for_Home_indicator], rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create two buttons separated by spacer item.
UIBarButtonItem *analyzeButton = [[UIBarButtonItem alloc] initWithTitle:#"Analyze" style:UIBarButtonItemStylePlain target:self action:#selector(analyze_clicked:)];
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:#"Clear" style:UIBarButtonItemStylePlain target:self action:#selector(clear_clicked:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; //Load NSUserDefaults.
NSMutableArray *objectArrayMutable = [[NSMutableArray alloc]init];
objectArrayMutable = [[prefs objectForKey:#"savedFavoritesArray"] mutableCopy];
int favoriteChosenAlready = 0;
if ([objectArrayMutable count] > 0) {
for (int arrayCounter = 0; arrayCounter < [objectArrayMutable count]; arrayCounter++) {
if ([[objectArrayMutable objectAtIndex:arrayCounter] integerValue] == 58) {
favoriteChosenAlready = 1;
}
}
}
UIButton *favButton = [[UIButton alloc] init];
if (favoriteChosenAlready == 0) {
//Display empty star.
[favButton setImage:[UIImage imageNamed:#"Star_button_empty.png"] forState:UIControlStateNormal];
}
else {
//Display filled star.
[favButton setImage:[UIImage imageNamed:#"Star_button_filled.png"] forState:UIControlStateNormal];
}
[favButton addTarget:self action:#selector(favorite_clicked:) forControlEvents:UIControlEventTouchUpInside];
[favButton setFrame:CGRectMake(280, 25, 30, 30)];
UIBarButtonItem *favoriteButton = [[UIBarButtonItem alloc] initWithCustomView:favButton];
[[UIBarButtonItem appearance] setTintColor:[UIColor lightGrayColor]]; //Make toolbar button text lightGrayColor.
[toolbar setItems:[NSArray arrayWithObjects:analyzeButton,flexItem,favoriteButton,flexItem,clearButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
To clarify, the Clear button here now is unresponsive:
[toolbar setItems:[NSArray arrayWithObjects:analyzeButton,flexItem,favoriteButton,flexItem,clearButton,nil]];
However, just rearranging the sequence of buttons in the toolbar fixes the problem:
[toolbar setItems:[NSArray arrayWithObjects:analyzeButton,clearButton,favoriteButton,flexItem,flexItem,nil]];
Related
I'm using the last two lines of the code below to change the tint colour of my navigation bar so that my UIBarButtonItems display as black instead of the default blue. This code works in another controller but not in this controller.
When I navigate forward to that controller then navigate back to the controller in question then the UIBarButtonItems are black as needed. However when I first load the app and this view is loaded they are blue.
How do I correctly change the tint colour of my navigation bar so my UIBarButtonItem's display as black?
The actual images I've set for the buttons are black.
Code in viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Logo button
UIButton *logoButton = [[UIButton alloc] init ];
[logoButton setImage:[UIImage imageNamed:#"va_logohme.png"] forState:UIControlStateNormal];
[logoButton setAdjustsImageWhenHighlighted:NO]; ;
[logoButton setFrame:CGRectMake(0, 0, 320, 40)];
[logoButton addTarget:self action:#selector(logoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[[self navigationItem] setTitleView:logoButton];
// Additional UIBarButtonItem's
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"hamburger_for_more.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuButtonTap)];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"magnify_glass.png"] style:UIBarButtonItemStylePlain target:self action:#selector(searchButtonTapped)];
[[self navigationItem] setLeftBarButtonItems:#[menuButton, searchButton]];
UIBarButtonItem *shoppingCartButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"shopping_bag.png"] style:UIBarButtonItemStylePlain target:self action:#selector(shoppingCartButtonTapped)];
UIBarButtonItem *addFavouriteButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"add_fav_heart.png"] style:UIBarButtonItemStylePlain target:self action:#selector(favouritesPageButtonTapped:)];
NSMutableArray *buttons = [[NSMutableArray alloc] init];
for (UIControl *btn in self.navigationController.navigationBar.subviews) {
if ([btn isKindOfClass:[UIControl class]]) {
[buttons addObject:btn];
}
}
// Basket container
_bagContainer = [[UIView alloc] initWithFrame:CGRectMake(277, 16, 21, 21)];
[[[self navigationController] navigationBar] addSubview:_bagContainer];
UILabel *bagCount = [[UILabel alloc] init];
[bagCount setText: [NSString stringWithFormat:#"%i", [Bag totalItems:[self managedObjectContext]]]];
[bagCount setFont:[UIFont systemFontOfSize:14]];
[bagCount sizeToFit];
[_bagContainer addSubview:bagCount];
bagCount.center = [_bagContainer convertPoint:_bagContainer.center fromView:_bagContainer.superview];
[_bagContainer setUserInteractionEnabled:NO];
[[self navigationItem] setRightBarButtonItems:#[shoppingCartButton, addFavouriteButton] animated: YES];
// Change colour of nav bar tint
[[[self navigationController] navigationBar] setBarTintColor:[UIColor whiteColor]];
[[[self navigationController] navigationBar] setTranslucent:NO];
}
If the images are template images, then set their tintColor to the desired color. If they are normal images, then make sure to render them as normal images so that they show up in their own color and not as tinted template images.
I have an existing code which was working fine in ios6. but in ios7 left most item("refresh button") is not showing align to other two UIBarButtonItem . its showing little down. here is the code for iOS6. what changes do i need to make this working in iOS7.
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem* refreshButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refreshButtonClicked:)];
refreshButton.style=UIBarButtonItemStyleBordered;
//self.navigationItem.rightBarButtonItem = refreshButton;
[buttons addObject:refreshButton];
[refreshButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard delete button with the trash icon
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button addTarget:self action:#selector(InfoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
infoItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttons addObject:infoItem];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar]autorelease];
Thanks
Try:
self.navigationItem.rightBarButtonItems = buttons;
after going through the questions related to UINavigationController item i came to post my exact problem ..
firstly i am not add a UINavigationController in MainWindow.xib i created and adding UINavigationController in AppDelegate file
after that in a UIViewController (not in rootViewController) class I have to add a rightbarbutton of Done type I am
adding it in a searchbarDelegate Method showing as bold:-
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
//This method is called again when the user clicks back from teh detail view.
//So the overlay is displayed on the results, which is something we do not want to happen.
if(exhibDataObj.searching)
return;
//Add the overlay view.
if(ovController == nil)
ovController = [[OverlayViewController alloc] initWithNibName:#"OverlayView" bundle:[NSBundle mainBundle]];
CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
//Parameters x = origion on x-axis, y = origon on y-axis.
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;
ovController.view.backgroundColor = [UIColor grayColor];
ovController.view.alpha = 0.5;
ovController.rvController = self;
[self.theTableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];
exhibDataObj.searching = true;
letUserSelectRow = NO;
self.theTableView.scrollEnabled = NO;
//Add the done button.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(doneSearching_Clicked:)];
}
but this never works .
Any help will be more appreciable.
I've tested your code real quick and it seems to be working fine for me, however, I've had the same problem once back then it turned out I was setting the rightBarButtonItem to nil in my viewDidLoad method after setting it to a button. you should make sure your not making the same mistake somehow.
However if that does not work for you you can always try setting a custom button to it:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 30, 30)];
button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"done.png"]];
[button addTarget:self action:#selector(doneButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = customItem;
I created an application with a side menu like facebooks. I have a root view controller which adds a view over itself, then when the user presses the 'Menu' button it will slide the view over so you can see the menu.
My issue is that when the view first loads it looks like the UINavBar is about 20 pixels or so lower than it should be. (The below image looks a bit off centered because it is a modal horizontal flip)
It then automatically adjusts itself and moves up just under that status bar when the modal transition is complete.
Here is a screenshot of what happens when I press the "Menu" button. It slides the top view over to the right
As you can see in the images above I have the left arrow and right arrow frame background set to red so that I can see where the clickable / tappable region is. I have another image which I drew a blue line on that shows if I click the blue line or above then it will not trigger the right arrow. If I click in that same general area on the left side the menu button will trigger. I am able to reproduce this in both the simulator and on a mobile device (so I am not fat fingering the button).
It seems to me that since the navigation bar is loading lower than it is supposed to the iPhone thinks it is still in that position so when I click inside the UIButton frame it is still triggering the Menu and not triggering the arrow selectors. If I tap DIRECTLY in the middle of the arrows, it triggers the arrows selectors but just a tiny bit outside the image, it will not trigger the arrow selector.
Here is my RootViewController viewDidLoad
AppointmentViewController *appointmentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"AppointmentViewController"];
NSLog(#"B: %#",NSStringFromCGRect(appointmentViewController.view.frame));
self.navController = [[UINavigationController alloc] initWithRootViewController:appointmentViewController];
NSLog(#"B2: %#",NSStringFromCGRect(appointmentViewController.view.frame));
NSLog(#"Nav Bounds: %#",NSStringFromCGRect(self.navController.view.frame));
NSLog(#"Bounds: %#",NSStringFromCGRect(self.contentView.bounds));
self.navController.view.frame = self.contentView.bounds;
NSLog(#"Nav Bounds2: %#",NSStringFromCGRect(self.navController.view.frame));
[self addShadowToMenu];
[self.contentView addSubview:self.navController.view];
RootViewController Logs
2012-04-30 12:24:44.533 My-App[19929:fb03] B: {{0, 20}, {320, 460}}
2012-04-30 12:24:44.535 My-App[19929:fb03] B2: {{0, 20}, {320, 460}}
2012-04-30 12:24:44.535 My-App[19929:fb03] Nav Bounds: {{0, 0}, {320, 480}}
2012-04-30 12:24:44.535 My-App[19929:fb03] Bounds: {{0, 0}, {320, 460}}
2012-04-30 12:24:44.535 My-App[19929:fb03] Nav Bounds2: {{0, 0}, {320, 460}}
AppointmentViewController
self.title = [dataObj.preferences objectForKey:#"appts_name_plural"];
self.navigationItem.title = self.title;
dayView = [[APCalendarDayView alloc] initWithCalendarView:(APCalendarView *)self];
APListView = [self.storyboard instantiateViewControllerWithIdentifier:#"AppointmentListView"];
APListView.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
SideMenuView *myDelegate = [[SideMenuView alloc] init];
[self setSideMenuDelegate:myDelegate];
//set the delegate's currentViewController property so that we can add a subview to this View.
[sideMenuDelegate setCurrentViewController:self];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *today = [[UIBarButtonItem alloc] initWithTitle:#"Today" style:UIBarButtonItemStyleBordered target:nil action:#selector(todayPressed)];
NSArray *appointmentNavigationItems = [[NSArray alloc] initWithObjects:#"Day",#"List",#"Month", nil];
navcontrol = [[UISegmentedControl alloc] initWithItems:appointmentNavigationItems];
[navcontrol setSegmentedControlStyle:UISegmentedControlStyleBar];
[navcontrol addTarget:self action:#selector(appointmentNavigationClicked:) forControlEvents:UIControlEventValueChanged];
[navcontrol setSelectedSegmentIndex:0];
UIBarButtonItem *segmentItems = [[UIBarButtonItem alloc] initWithCustomView:navcontrol];
NSArray *items = [NSArray arrayWithObjects:today,flexSpace,segmentItems, flexSpace, nil];
[appointmentToolbar setItems:items animated:NO];
[appointmentToolbar setAutoresizesSubviews:YES];
[[self.view superview] addSubview:myDelegate.view];
[self setViewSizeOffset:(appointmentToolbar.bounds.size.height)];
CalendarDayView
- (void)addSubviewsToHeaderView:(UIView *)headerView
{
const CGFloat kChangeMonthButtonWidth = 46.0f;
const CGFloat kChangeMonthButtonHeight = 44.0f;
const CGFloat kMonthLabelWidth = 215.0f;
const CGFloat kHeaderVerticalAdjust = 7.f;
// Header background gradient
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Kal.bundle/kal_grid_background.png"]];
CGRect imageFrame = headerView.frame;
imageFrame.origin = CGPointZero;
backgroundView.frame = imageFrame;
[headerView addSubview:backgroundView];
[backgroundView setAutoresizesSubviews:YES];
[backgroundView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
// Create the previous month button on the left side of the view
CGRect previousMonthButtonFrame = CGRectMake(self.left, 0, kChangeMonthButtonWidth, kChangeMonthButtonHeight);
UIButton *previousMonthButton = [[UIButton alloc] initWithFrame:previousMonthButtonFrame];
[previousMonthButton setBackgroundColor:[UIColor redColor]];
[previousMonthButton setImage:[UIImage imageNamed:#"Kal.bundle/kal_left_arrow.png"] forState:UIControlStateNormal];
previousMonthButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
previousMonthButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[previousMonthButton addTarget:self action:#selector(showPreviousDay) forControlEvents:UIControlEventTouchUpInside];
//[previousMonthButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin];
[headerView addSubview:previousMonthButton];
// Draw the selected month name centered and at the top of the view
CGRect monthLabelFrame = CGRectMake((self.frame.size.width/2.0f) - (kMonthLabelWidth/2.0f), kHeaderVerticalAdjust, kMonthLabelWidth, kMonthLabelHeight);
headerTitleLabel = [[UILabel alloc] initWithFrame:monthLabelFrame];
headerTitleLabel.backgroundColor = [UIColor clearColor];
headerTitleLabel.font = [UIFont boldSystemFontOfSize:22.f];
headerTitleLabel.textAlignment = UITextAlignmentCenter;
headerTitleLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Kal.bundle/kal_header_text_fill.png"]];
headerTitleLabel.shadowColor = [UIColor whiteColor];
headerTitleLabel.shadowOffset = CGSizeMake(0.f, 1.f);
//[self setHeaderTitleText:[logic selectedMonthNameAndYear]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"E, MMM dd, yyyy"];
headerTitleLabel.text = [dateFormatter stringFromDate:dataObj.activeDate];
[headerView addSubview:headerTitleLabel];
// Create the next month button on the right side of the view
CGRect nextMonthButtonFrame = CGRectMake(self.frame.size.width - kChangeMonthButtonWidth, 0, kChangeMonthButtonWidth, kChangeMonthButtonHeight);
UIButton *nextMonthButton = [[UIButton alloc] initWithFrame:nextMonthButtonFrame];
nextMonthButton.frame = nextMonthButtonFrame;
[nextMonthButton setImage:[UIImage imageNamed:#"Kal.bundle/kal_right_arrow.png"] forState:UIControlStateNormal];
[nextMonthButton setBackgroundColor:[UIColor redColor]];
nextMonthButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
nextMonthButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[nextMonthButton addTarget:self action:#selector(showFollowingDay) forControlEvents:UIControlEventTouchUpInside];
//[nextMonthButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[headerView addSubview:nextMonthButton];
}
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.frame.size.width, kHeaderHeight)];
headerView.backgroundColor = [UIColor grayColor];
[self addSubviewsToHeaderView:headerView];
[headerView setAutoresizesSubviews:YES];
[headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self addSubview:headerView];
[self bringSubviewToFront:headerView];
If I move the headerView down farther it will allow me to select anywhere in the red area and have the arrow selector be triggered. However, I want it to match Apples calendar and be just under the navigation bar.
Anyone know why the navigation bar is blocking the top portion of the headerView from being tapped? To be clear the portion of the headerView that is being blocked is from the bottom of the blue line and up. The UIButton with the red background has an action tied to the frame so it should be triggering the event, however it appears the navbar is blocking half of the UIButton from receiving taps.
This behavior is due to the fact that UIWindow is not propagating touches that coincide with the nav bar frame. The only way to override this behavior is to subclass UIWindow and override the hitTest: method. This will allow you to detect touches in this area, and forward them to the appropriate responders.
Here is an example project showing a custom UIWindow subclass that will respond to touches any where on the screen.
I have customized the UINavigationBar's height to 100px and I'd like to add buttons onto this customized bar.
All is good except the button seems to want to sit on the bottom of the nav bar no matter what. I can't get it to align to the center or the top of the nav bar.
Here is the code; first I create a navigation controller in the app delegate and add one button on the right.
// set main navigation controller
temp *tempc = [[temp alloc] initWithNibName:#"temp" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tempc];
UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
[tempc.navigationItem setRightBarButtonItem:navItem];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
then I resize the navigation bar in the "temp" view controller like so:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 100.0f);
[self.navigationController.navigationBar setFrame:frame];
[self.navigationController.navigationBar setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
}
I've also tried to add a custom view to the rightBarButtonItem but I can't get the added custom view to touch the top completely.
And the code for this unfortunate attempt:
// set main navigation controller
temp *tempc = [[temp alloc] initWithNibName:#"temp" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tempc];
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36.0f, 80.0f)];
[customView setBackgroundColor:[UIColor blueColor]];
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setFrame:CGRectMake(0, 22.0f, 36.0f, 36.0f)];
[customButton setTitle:#"+" forState:UIControlStateNormal];
[customView addSubview:customButton];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
Does anyone know how to vertically align UIBarButtonItems on a UINavigationBar?
This will move page title and all buttons up 20 px:
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
The current accepted answer from alhcr (Option 1) does not work if you only have a rightBarButtonItem and no left. It is also making an assumption about the subview ordering of the UINavigationBar which is not guaranteed and could very well change in a future version of iOS. Here is a better way of customizing navigation button frames:
// UINavigationBar subclass
- (void)layoutSubviews {
[super layoutSubviews];
UINavigationItem *navigationItem = [self topItem];
for (UIView *subview in [self subviews]) {
if (subview == [[navigationItem rightBarButtonItem] customView]) {
CGRect newRightButtonRect = CGRectMake(...new rect...);
[subview setFrame:newRightButtonRect];
}
else if (subview == [[navigationItem backBarButtonItem] customView]) {
CGRect newBackButtonRect = CGRectMake(...new rect...);
[subview setFrame:newBackButtonRect];
}
}
}
Option 1:
create UINavigationBar subclass
inside this class override - (void)layoutSubviews where you will reposition UIBarButtonItems
in Interface Builder set UINavigationBar class to UINavigationBar subclass
Example:
inside subclass of UINavigationBar:
- (void)layoutSubviews {
int index = 0;
for ( UIButton *button in [self subviews] ) {
if(index == 1) {
[button setFrame:CGRectMake(5,40,60,40)]; //leftBarButtonItem
} else if(index == 2) {
[button setFrame:CGRectMake(275,40,40,40)]; //rightBarButtonItem
}
++index;
}
}
view controller:
- (void)viewDidLoad {
...
UIBarButtonItem *navItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
UIBarButtonItem *navItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
[self.navigationItem setRightBarButtonItem:navItem1];
[self.navigationItem setLeftBarButtonItem:navItem2];
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 100.0f);
[self.navigationController.navigationBar setFrame:frame];
...
}
Option 2 (inserting UIBarButtonItem doesn't work):
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,320,100)];
[self.view addSubview:navigationBar];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0,25,50,50)];
[navigationBar addSubview:button];
I found the solution of this problem by making adjustment in the Image Edge Insets of the custom button. I had the requirement in the app to increase the height of the navigation bar and after increasing the height makes the rightBarButtonItem and leftBarButtonItem images unpositioned problem.
Find the code below:-
UIImage *image = [[UIImage imageNamed:#"searchbar.png"];
UIButton* searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchbutton addTarget:self action:#selector(searchBar:) forControlEvents:UIControlEventTouchUpInside];
searchbutton.frame = CGRectMake(0,0,22, 22);
[searchbutton setImage:image forState:UIControlStateNormal];
[searchbutton setImageEdgeInsets:UIEdgeInsetsMake(-50, 0,50, 0)];
// Make BarButton Item
UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchbutton];
self.navigationItem.rightBarButtonItem = navItem;