导航栏barbuttonitem IOS 11和xcode 9中的错误帧

时间:2017-11-22 06:26:39

标签: ios xcode uinavigationbar uibarbuttonitem ios11

我有一个Viewcontroller,我在其中设置了社交媒体的三个右侧Navbar按钮。以前我在Xcode 8中运行应用程序。但是现在当我将Xcode更新为9.1时,UI被打扰了。 Navbar更改其宽度,现在在整个Navbar中传播。代码设置为宽度但不按代码运行。我的代码就是这个,

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

现在当我在Xcode 9.1中运行应用程序时,它会显示我的Navbar按钮, enter image description here

1 个答案:

答案 0 :(得分:2)

我认为在ios 11 barbuttonitems不使用框架。你必须设置必要的约束。这里你的按钮宽度不合适,所以一旦尝试设置宽度,

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

为每个按钮设置宽度常量!

执行此操作后,如果您遇到高度问题,请对heightAnchor执行相同的操作。

这种情况正在发生,因为您的图像必须大于按钮的大小,并且您没有对高度和宽度进行约束,因此您的按钮会调整大小以调整图像大小!