We were using the below code to show leftbarbutton item in our navigation bar in IOS 10 SDK and XCode 8, however when we updated to xcode9 and ios 11 sdk. The left bar button item doesnt show. I searched through the web , but couldnt find any solution to that.
Does anyone know any answer?
UIBarButtonItem *cancelButton = nil;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"11")) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
//button.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
//button.contentEdgeInsets = UIEdgeInsetsMake(13, 8, 13, 8);
NSLayoutConstraint *widthConst = [button.widthAnchor constraintEqualToConstant:32.0];
NSLayoutConstraint *heightConst = [button.heightAnchor constraintEqualToConstant:32.0];
[button setImage:[UIImage imageNamed:#"cross"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
cancelButton = [[UIBarButtonItem alloc] initWithCustomView:button];
widthConst.active = YES;
heightConst.active = YES;
} else {
cancelButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"cross"] style:UIBarButtonItemStylePlain target:loginRouter action:#selector(hide)];
}
cancelButton.tintColor = [UIColor whiteColor];
loginViewController.navigationItem.leftBarButtonItem = cancelButton;
loginRouter.loginViewController = loginViewController;
CardNavigationController *navigationController = [CardNavigationController darkBlueNavigationControllerWithClient:client];
navigationController.viewControllers = #[ loginViewController ];
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[onViewController presentViewController:navigationController animated:YES completion:nil];
And you can find the result in ios 11 below
Check the following example
UIImage *imgCart = [self imageWithImage:[UIImage imageNamed:#"ic_cart"] scaledToSize:CGSizeMake(35, 35)] ;
imgCart = [_utils changeColorOf:imgCart to:[UIColor whiteColor]];
UIButton *btnCart = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnCart addTarget:self action:#selector(btnCartClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnCart setBackgroundImage:imgCart forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnCart];
It does not work when you set 'cancelButton.tintColor = [UIColor whiteColor]' in ios11. you can use the method "setTitleTextAttributes".
Related
i want to ask how i can get the icon of navigationtabbaritem to the left corner (left:0) and right corner (right:0).
my code :
_custombtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
_custombtn.backgroundColor = [UIColor redColor];
[_custombtn setBackgroundImage:[UIImage imageNamed:#"ic_msg.png"]forState:UIControlStateNormal];
[_custombtn addTarget:self action:#selector(viewBn:) forControlEvents:UIControlEventTouchUpInside];
self.csbtn = [[UIBarButtonItem alloc]initWithCustomView:_custombtn];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(0.0, 0.0, 44, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *spaceFix = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
spaceFix.width = -10;
self.navigationItem.rightBarButtonItems = #[spaceFix, aBarButtonItem];
Please add spaceFix for resolve your problem may this help lot
For some reason the label in my navigation bar keeps disappearing and only displaying my map icon and home button. Is it because I'm adding it as a barbuttonitem? Is there a way around this?
UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(160, 5, 250, 70)];
UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(0,5, 310, 70)];
l1.text=#"FIND THINGS TO DO";
l1.font=[UIFont fontWithName:#"Helvetica Neue LT Std" size:25.0];
l1.backgroundColor=[UIColor clearColor];
l1.textColor=[UIColor whiteColor];
l1.textAlignment=UITextAlignmentCenter;
[view1 addSubview:l1];
UIBarButtonItem *left2=[[UIBarButtonItem alloc]initWithCustomView:view1];
self.navigationItem.rightBarButtonItem=left2;
[self.navigationItem setHidesBackButton:YES];
self.navigationItem.hidesBackButton=YES;
self.navigationController.navigationBarHidden = NO;
UIImage* image3 = [UIImage imageNamed:#"home_btn.png"];
homeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 38, 38)];
[homeBtn setBackgroundImage:image3 forState:UIControlStateNormal];
[homeBtn addTarget:self action:#selector(Home)
forControlEvents:UIControlEventTouchUpInside];
[homeBtn setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *home = [[UIBarButtonItem alloc] initWithCustomView:homeBtn];
NSArray *arr=[[NSArray alloc]initWithObjects:home, nil];
self.navigationItem.rightBarButtonItems = arr;
UIImage* mapButton1 = [UIImage imageNamed:#"Top-Map-Icon.png"];
mapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 26, 30)];
[mapButton setBackgroundImage:mapButton1 forState:UIControlStateNormal];
[mapButton addTarget:self action:#selector(Map)
forControlEvents:UIControlEventTouchUpInside];
[mapButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *map = [[UIBarButtonItem alloc] initWithCustomView:mapButton];
NSArray *arr2 = [[NSArray alloc] initWithObjects:map, nil];
self.navigationItem.leftBarButtonItems = arr2;
UIMapViewController *mapViewContr = [[[UIMapViewController alloc] initwithClientKey:clientKey] autorelease];
UINavigationController *nc = [[[UINavigationController alloc] initWithRootViewController:mapViewContr] autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 44, 44);
[button setImage:[UIImage imageNamed:#"info2.png"] forState:UIControlStateNormal];
UIBarButtonItem *lbutton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
nc.navigationItem.leftBarButtonItem = lbutton;
above codes not work, the leftBarButtonItem not show.but when i set leftBarButtonItem in function: UIMapViewController.vieDidLoad , the leftBarButtonItem show . why?
the flowing codes is in UIMapViewController.vieDidLoad
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(0, 0, 44, 44);
// [button setBackgroundImage:[UIImage imageNamed:#"info2.png"] forState:UIControlStateNormal];
[_button setImage:[UIImage imageNamed:#"info2.png"] forState:UIControlStateNormal];
UIBarButtonItem *lbutton = [[[UIBarButtonItem alloc] initWithCustomView:_button] autorelease];
self.navigationItem.leftBarButtonItem = lbutton;
In the first block of code you are calling:
nc.navigationItem.leftBarButtonItem = lbutton;
This should be:
mapViewContr.navigationItem.leftBarButtonItem = lbutton;
You always want to reference the navigationItem of the view controller, not the navigation controller.
iPad app:I've add some customized button to navigationBar as follows:
- (void)addEditButton{
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, 50, 30);
[editButton setBackgroundImage:[[UIImage imageNamed:#"images_iPad.bundle/NavigationBar_Btn_iPad.png"] stretchableImageWithLeftCapWidth:15/2.0 topCapHeight:32/2.0] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
[editButton setTitle:#"Edit" forState:UIControlStateNormal];
editButton.titleLabel.font = [UIFont fontWithName:kAPPBOLDFONT size:12];
editButton.titleLabel.shadowColor = [UIColor blackColor];
editButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
UIBarButtonItem *editItem = [[[UIBarButtonItem alloc] initWithCustomView:editButton] autorelease];
self.navigationItem.rightBarButtonItem = editItem;
}
My problem is that:I clicked this button,it will push to another viewController, after popModelViewController:animate,I can't see this customized button, although this, it does work.and not just here ,others are the same,please help me.
I have added a few buttons to the right side of the navigation bar with the following:
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
customView.backgroundColor = [UIColor clearColor];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:#"toc.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(tableOfContentsAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:#"bookmark.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(bookmarkButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[customView release];
[segmentBarItem release];
This works well. For both buttons I show a popOver as shown below
- (void) bookmarkButtonAction
{
BookmarksViewController* content = [[BookmarksViewController alloc] initWithOrientation:lastOrientation selectedPage:selectedPage];
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:content];
CGSize size = content.view.frame.size;
aPopover.popoverContentSize = size;
aPopover.delegate = self;
self.bookmarksPopoverVC = content;
self.bookmarksPopoverVC.popUpController = aPopover;
[content release];
[aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[aPopover release];
bookmarksShowing = YES;
}
The problem is that I am using presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem and this shows the top arrow in the middle of the two buttons. How can I attach the arrow to each button?
instead of using this line:
aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem
You may better try this line:
aPopover presentPopoverFromBarButtonItem:sender
I think that would solve your problem
try this:
- (IBAction)products:(id)sender {
UIButton* btn = (UIButton *)sender;
[productsPopover presentPopoverFromRect:[btn bounds] inView:btn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Works like a charm