IOS 11 和 xcode 9 中的 Navigationbar barbuttonitem 错误框架

Navigationbar barbuttonitem wrong frame in IOS 11 and xcode 9

我有一个 Viewcontroller,我在其中设置了社交媒体的三个右导航栏按钮。以前我在 Xcode 8 中使用 运行ning 应用程序。但是现在当我将 Xcode 更新到 9.1 时,UI 受到了干扰。 Navbar 改变了它的宽度,现在扩展到整个 Navbar。代码是为宽度设置的,但不是 运行ning 根据代码。我的代码是这样的,

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 中的应用程序时,它会像这样显示我的导航栏按钮,

我认为在 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 执行相同的操作。

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