How to make NavigationBar rotate with device - ios

I have added a navigation bar programmatically to my app as follows:
//Setup Navigationbar:
navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [[UIScreen mainScreen ] bounds].size.width, 44)];
UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:#"Navigation Title"];
//Navigationbar left items:
UIButton *fileButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fileButton addTarget:self action:#selector(loadFile:) forControlEvents:UIControlEventTouchUpInside];
[fileButton setImage:[UIImage imageNamed:#"menuicon.png"] forState:UIControlStateNormal];
fileButton.frame = CGRectMake(0, 0, 24, 24);
fileButton.backgroundColor=[UIColor clearColor];
newItem = [[UIBarButtonItem alloc] initWithCustomView:fileButton];
navBar.items = [NSArray arrayWithObjects: navigItem,nil];
//NavigationBar appearance:
[navBar setBackgroundImage:[UIImage imageNamed:#"greenbar.png"] forBarMetrics:UIBarMetricsDefault];
[self.window addSubview: navBar];
[self setTitle:#"Empty"];
Problem is i can't get it to rotate with the device, As you can see im adding the bar to the windows of my AppDelegate. How do i make it follow the rotation and autoresize?

To enable the rotation of the navbar it needs to be added to a view that reacts on the rotation of the device. I fixed the issue by switching from [self.window addSubview: navBar]; to [self.window.rootViewController.view addSubview: navBar];

Related

UINavigationItems Stretching in iOS11

I'm updating an app to ensure that it works as expected in iOS11. The only issue I've run into is that the navigation bar looks quite different in iOS11. The position of the elements and size is not the same between versions. I've looked at my code and i'm explicitly setting frame sizes and edge insets, why are these values are not respected in iOS11?
//-- Create Right Navigation Item Buttons
// create kabob btn
UIButton *kabobBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[kabobBtn addTarget:self action:#selector(viewUsersClicked:)forControlEvents:UIControlEventTouchUpInside];
kabobBtn.clipsToBounds = YES;
[kabobBtn setImage:[UIImage imageNamed:#"KabobMenu2_0071bc_Right.png"]
forState:UIControlStateNormal];
[kabobBtn.imageView setContentMode:UIViewContentModeScaleAspectFit];
[kabobBtn setFrame:CGRectMake(0, 0, 32, 32)];
// create user profile button
UIButton *userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[userBtn setImage:[UIImage imageNamed:#"people.png"]
forState:UIControlStateNormal];
[userBtn addTarget:self action:#selector(viewUserProfileClicked:)forControlEvents:UIControlEventTouchUpInside];
[userBtn setFrame:CGRectMake(0, 0, 38, 38)];
userBtn.clipsToBounds = YES;
[userBtn.layer setCornerRadius:(38/2)];
[userBtn.layer setMasksToBounds:YES];
[userBtn.layer setBorderColor:[[UIColor blackColor]CGColor]];
[userBtn.layer setBorderWidth:1];
self.userPictureURL = [defaults objectForKey:#"userPicture"];
[userBtn setImage:[UIImage imageNamed:#"defaultUserImage.png"] forState:UIControlStateNormal];
[userBtn setContentMode:UIViewContentModeScaleAspectFill];
[userBtn setBackgroundColor:[UIColor clearColor]];
// add buttons to bar button items
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:kabobBtn];
UIBarButtonItem *barButton2 = [[UIBarButtonItem alloc] initWithCustomView:userBtn];
// add bar button items to right bar button item array
self.navigationItem.rightBarButtonItems = #[barButton,barButton2];
//-- Create Left Hamburger Button
UIButton *customOpen = [UIButton buttonWithType:UIButtonTypeCustom];
[customOpen setFrame:CGRectMake(0, 0, 30, 44)];
[customOpen setImage:[UIImage imageNamed:#"hamburger.png"] forState:UIControlStateNormal];
[customOpen setImageEdgeInsets:UIEdgeInsetsMake(10, -5, 16, 10)];
UIBarButtonItem *openItem = [[UIBarButtonItem alloc] initWithCustomView:customOpen];
if(self.showHamburger){
self.navigationItem.leftBarButtonItem = openItem;
}else{
self.navigationItem.leftBarButtonItem = nil;
}
Apparently iOS 11 uses autolayout for nav bar buttons. Use auto layout constraints to set button width and height.
[userBtn.widthAnchor constraintEqualToConstant:38.f].active = YES;
[userBtn.heightAnchor constraintEqualToConstant:38.f].active = YES;
This should fix the stretching issue.
Thanks to this answer

How to add multiple bar buttons to navigation bar

Initially i have created a dynamic view called 'mainView'.In that,i have added Navigation bar.After that, i Want to add 2 right bar buttons to the navigation bar.If i code like this,its shows view with navigation bar.but its not showing right bar button.
1.Is it possible to add navigation bar without navigation controller?
2.Why the right bar button is not showing?
//mainView Creation
UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
mainView.backgroundColor =[UIColor grayColor];
[self.view addSubview:mainView];
//NavigationBar
UINavigationBar *NavigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
NavigationBar.backgroundColor = [UIColor orangeColor];
[mainView addSubview:NavigationBar];
//rightbarView Creation
UIView *rightbarView = [[UIView alloc] init];
rightbarView.backgroundColor = [UIColor orangeColor];
//powerCut Button
UIButton *powerCutbutton = [[UIButton alloc] initWithFrame:CGRectMake(150, 20, 50, 14.01)];
[rightbarView addSubview:powerCutbutton];
UIImage *powercutimage = [UIImage imageNamed:#"powercut.jpg"];
[powerCutbutton setImage:powercutimage forState:UIControlStateNormal];
// addConnection button
UIButton *addConnectionbutton = [[UIButton alloc] initWithFrame:CGRectMake(210, 20, 50, 14.01)];
[rightbarView addSubview:addConnectionbutton];
UIImage *addimage = [UIImage imageNamed:#"add.png"];
[addConnectionbutton setImage:addimage forState:UIControlStateNormal];
// Barbutton item
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightbarView];
//[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:powerCutbutton, addConnectionbutton, nil]];
// setting rightbar button item
self.navigationItem.rightBarButtonItem = item;
create an array of bar buttons. and setLeftBarButtonItems not setLeftBarButtonItem as :
UIBarButtonItem * barbtn1;
UIBarButtonItem * barbtn2;
UIBarButtonItem * barbtn3;
[self.navigationItem setLeftBarButtonItems:#[barbtn1,barbtn2,barbtn3]];
or
[self.navigationItem setRightBarButtonItems:#[barbtn1,barbtn2,barbtn3]];
hope it will helpful for you

Title of UINavigationBar becomes blank when segueing back to root VC

I've got a UINavigationController that segues to 2 other VC's. Whenever it reaches a certain VC with the current setup :
// set text
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.title = #"Title";
// make the nav bar transaparent
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setOpaque:NO];
// set white status bar
self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
[self setNeedsStatusBarAppearanceUpdate];
//self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(16, 31, 22, 20)];
UIImage *backImage = [UIImage imageNamed:#"backButtonDetail"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
And I pop the VC, the title of the root vc becomes empty. However, whenever I'm segueing to a VC without this setup, but a more 'basic' setup, the title stays where it should.
Easiest thing I can recommend: use [UINavigationBar appearance] to set many of these customizations from the AppDelegate
Checkout out AppCoda's post on the subject. I've just run through it myself and it's pretty great

UIBarButtonItem backButton setting image with no back text causes title alignment issues

I am wanting to use an image for my backButton in my navigationController. I just want the image used, with no "back" text shown at all, so I did:
UIImage *backButtonImage = [[UIImage imageNamed:#"ZSSBackButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 25, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault];
This works well, but seems to have the side effect to making long navigationItem titles appear to be off center, even though it really is just leaving white space where the backbutton text should be:
Is there a way that I can bring the title a little more to the left?
You can manage position of UINavigationbar title using titleView property.
UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];// Here you can set View width and height as per your requirement for displaying titleLbl position in navigationbar
[backView setBackgroundColor:[UIColor clearColor]];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
[backView addSubview:titleLbl];
titleLbl.text = #"YOUR TITLE";
titleLbl.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = backView;
EDIT : you can create custom button and assign it to leftbutton as :
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0,0,51,31);
[btnBack setBackgroundImage:[UIImage imageNamed:#"ZSSBackButton"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(btnBackProfile_TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
[self.navigationItem setLeftBarButtonItem:leftBtn];
Hope it helps you.

Custom UIButton not working

After looking through the post on this simple topic I still can not figure out what I've done wrong here. I am trying to make the buttons loop through the same action in the same viewController.
When the view comes onto the screen the buttons do not click (or at least the NSLog does not write to the console which it does the first time it enters this code.)
- (IBAction)answer: (id) sender{
NSLog(#"The Number is: %d\n",qNumber);
qNumber+=1;
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[newView setUserInteractionEnabled:TRUE];
UIImage *buttonImage = [UIImage imageNamed:#"pink_button.png"];
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
[buttonImageView setImage:buttonImage];
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
buttonLabel.text = #"newLow";
buttonLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 15.0];
buttonLabel.textColor = [UIColor blackColor];
buttonLabel.backgroundColor = [UIColor clearColor];
buttonLabel.textAlignment = UITextAlignmentCenter;
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton addSubview:buttonImageView];
[lowButton addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
}
Thanks for any help you can provide...even if I missed something simple :-)
Latest Updated Code:
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newView addSubview:buttonImageView];
[newView addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
[self.view bringSubviewToFront:lowButton];
self.view = newView;
Still when the button is pressed it does not enter the action code.
You button needs a frame. You can't tap a frameless button.
lowButton.frame = CGRectMake(23, 294, 72, 37);
Also, when you add label and image to that button, their frame should have x and y 0.
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
But may I ask why you are adding new UIImageView and UILabel to UIButton when it has it's label and background view properties?
Edit here is a cleaned-up code for you:
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton setImage:[UIImage imageNamed:#"pink_button.png"] forState:UIControlStateNormal];
[lowButton setTitle:#"newLow" forState:UIControlStateNormal];
[lowButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[lowButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0]];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
Notice I also removed [newView setUserInteractionEnabled:TRUE]; as it's not needed since it's enabled by default.
Your custom button's subviews are covering the button, so it does not get any clicks.
Try adding all the subviews to newView rather than to lowButton (including lowButton itself) and then call
[self.view bringSubViewToFront:lowButton];

Resources