codepath / ios_guides

Comprehensive open-source iOS guides

Home Page:http://guides.codepath.com/ios

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Navigation Item Problem

NijaahnandhRV opened this issue · comments

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

Do you have a repro example?

This was taken in iPhone version >11.0
image

This was taken in iPhone version <11.0
image

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];```
commented

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)

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

commented

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.

Can you please tell me send me few examples please?

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

commented

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;

Ok thank you let me check it and revert you back

Thank you so much now its working correctly