codepath / ios_guides

Comprehensive open-source iOS guides
http://guides.codepath.com/ios
2.88k stars 472 forks source link

Navigation Item Problem #59

Closed NijaahnandhRV closed 6 years ago

NijaahnandhRV commented 6 years ago

Hi i have created a navigation button programmatically. I ran the code in two different versions of iPhone one is less than 10.0 and another is greater than 11.0. I'm getting the navigation button correctly in 10.0 but in 11.0 the button I have created is getting stretched like this many problems are coming

rogerhu commented 6 years ago

Do you have a repro example?

NijaahnandhRV commented 6 years ago

This was taken in iPhone version >11.0 image

This was taken in iPhone version <11.0 image

NijaahnandhRV commented 6 years ago

Code :


        UIButton* requestButton8a = [UIButton buttonWithType:UIButtonTypeCustom];
        requestButton8a.frame=CGRectMake(0, 0, 26, 26);
        [requestButton8a setImage:image2 forState:UIControlStateNormal];
        [requestButton8a addTarget:(DEMONavigationController *)self.navigationController action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *button24a = [[UIBarButtonItem alloc] initWithCustomView:requestButton8a];
        [self.navigationItem setLeftBarButtonItem:button24a];```
jurajantas commented 6 years ago

to make it work on iOS11 you need to add constraints for size of the button. This is due to changes for expanding navigation bar (the new style and behavior for Large titles)

NijaahnandhRV commented 6 years ago

I have already given height and width of the button right? Then also it's coming like that!

jurajantas commented 6 years ago

you are setting the frame only. But that is useless in world of autolayout. You have few options: 1. subclass UIButton and override intrinsicContentSize function to return size, or 2. put size constraints to the button.

NijaahnandhRV commented 6 years ago

Can you please tell me send me few examples please?

NijaahnandhRV commented 6 years ago

Since I'm setting it programmatically, I don't know how to give autolayout. Could you please help me out!

jurajantas commented 6 years ago

Don't know what version of iOS are you targeting but easiest way (iOS9+) is to use anchors: [requestButton8a.widthAnchor constraintEqualToConstant:26].active = YES; [requestButton8a.heightAnchor constraintEqualToConstant:26].active = YES;

NijaahnandhRV commented 6 years ago

Ok thank you let me check it and revert you back

NijaahnandhRV commented 6 years ago

Thank you so much now its working correctly