【发布时间】:2017-11-22 06:26:39
【问题描述】:
我有一个 Viewcontroller,我在其中设置了三个社交媒体右导航栏按钮。以前我在 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]];
【问题讨论】:
-
为什么要为按钮设置框架?只需使用您的图像制作 UIBarButton。它会自动调整大小。
-
我有三个按钮。 @Muneeba
-
您正在使用 setRightBarButtonItems,它需要一个数组。因此,您可以将 UIBarButtons 添加到数组中并将其分配给 setRightBarButtonItems。您可以使用 UIBarButtonSystemItemFixedSpace 添加 UIbarbutton,并根据需要将其视为按钮之间的间隔。
标签: ios xcode uinavigationbar uibarbuttonitem ios11