2

I have a Viewcontroller in which i have set three right Navbar button of social media. Previously i was running app in Xcode 8. But now when i update the Xcode to 9.1 the UI got disturb. The Navbar change its width and now spread in whole Navbar. The code is set for width but its not running as per code. My code is this,

UIImage* image1 = [UIImage imageNamed:@"fab.png"];
CGRect frameimg1 = CGRectMake(3,0,30,30);
UIButton *someButton1 = [[UIButton alloc] initWithFrame:frameimg1];
[someButton1 setBackgroundImage:image1 forState:UIControlStateNormal];
[someButton1 addTarget:self action:@selector(facebook)
     forControlEvents:UIControlEventTouchUpInside];
[someButton1 setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton1 =[[UIBarButtonItem alloc] initWithCustomView:someButton1];

UIImage* image2 = [UIImage imageNamed:@"tt.png"];
CGRect frameimg2 = CGRectMake(20,50,30,30);
UIButton *someButton2 = [[UIButton alloc] initWithFrame:frameimg2];
[someButton2 setBackgroundImage:image2 forState:UIControlStateNormal];
[someButton2 addTarget:self action:@selector(twitter)
     forControlEvents:UIControlEventTouchUpInside];
[someButton2 setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton2 =[[UIBarButtonItem alloc] initWithCustomView:someButton2];

    UIImage *image3 = [UIImage imageNamed:@"G+.png"];
CGRect frameimg3 = CGRectMake(0,30,30,30);
UIButton *someButton3 = [[UIButton alloc] initWithFrame:frameimg3];
[someButton3 setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton3 addTarget:self action:@selector(gmail)
     forControlEvents:UIControlEventTouchUpInside];
[someButton3 setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton3 =[[UIBarButtonItem alloc] initWithCustomView:someButton3];


[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:mailbutton1,mailbutton2,mailbutton3, nil]];

Now when i run the app in Xcode 9.1 it shows my Navbar button like this, enter image description here

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
siddle
  • 137
  • 2
  • 10
  • why are you setting frames for buttons? just make UIBarButton with your images.It will adjust the size automatically. – Muneeba Nov 22 '17 at 06:34
  • i have three buttons. @Muneeba – siddle Nov 22 '17 at 06:49
  • You are using setRightBarButtonItems, it takes an array. So you can add your UIBarButtons in array and assign it to setRightBarButtonItems.You can add UIbarbutton with UIBarButtonSystemItemFixedSpace and treat as spacer between buttons if you want. – Muneeba Nov 22 '17 at 06:54

1 Answers1

2

I think in ios 11 barbuttonitems is not working with frames. you have to set necessary constraints. here your button's width is not proper so once try by setting width like,

[[someButton1.widthAnchor constraintEqualToConstant:30.0] setActive:YES];
[[someButton2.widthAnchor constraintEqualToConstant:30.0] setActive:YES];
[[someButton3.widthAnchor constraintEqualToConstant:30.0] setActive:YES];

set width constant for every button!

After doing this if you get issue of height then do same thing for heightAnchor.

This is happening because your image must be large then your button's size and you are not giving constraints for height and width so your buttons are getting resized to adjust image size!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • I have to set these constraints now with my code? @Lion – siddle Nov 22 '17 at 06:43
  • yes just add this line in your code after creating button. For, example you create `someButton1` - > set frame -> set all properties and then set it's width constraint like : `[[someButton1.widthAnchor constraintEqualToConstant:30.0] setActive:YES];`! – Ketan Parmar Nov 22 '17 at 06:45
  • This is creating a clash in constraints for me. There seems to be one already applied with "_UITemporaryLayoutWidth", that I can't figure out how to remove. I have tried iterating over the buttons existing constraints and removing widths and heights, but it doesn't seem to work. – narco Aug 27 '18 at 21:22
  • Following from above comment: I've at least gotten rid of the clash, by making sure the constraint is the same dimension as the actual UIImage on the button. The button is no longer resizing either. – narco Aug 27 '18 at 21:30