1

因此,当我尝试为 UIBarButton 设置自定义颜色时,它会导致显示的按钮太大,并向下延伸到 NavigationBar 并进入应用程序的主视图。有没有简单的方法来解决这个问题?这是我的代码:

UIImage *buttonColor = [[UIImage imageNamed:@"blue-background.jpg"]
                     resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

[[UIBarButtonItem appearance] setBackgroundImage:buttonColor forState:UIControlStateNormal
                                      barMetrics:UIBarMetricsDefault];
4

2 回答 2

0

尝试使用UIEdgeInsetsMake函数的值。在我的一个应用程序中,它看起来像这样;

UIImage *backbuttonimage = [[UIImage imageNamed:@"backbutton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backbuttonimage
                                                              forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

它如我所愿......如果您有任何问题,我希望您告诉我,如果您可以添加一张照片,让我更好地理解问题。

这些是我对我的应用程序的价值观:

UIEdgeInsetsMake(0, 15, 0, 10)

祝你今天过得愉快 :)

于 2013-05-26T00:09:44.067 回答
0

这就是我定制的方式UIBarButtonItem,它对我来说很完美。尝试这个:

UIButton *postView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 61, 30)];
[postView setTitle:@"Post" forState:UIControlStateNormal];
[postView addTarget:self action:@selector(showAddPost) forControlEvents:UIControlEventTouchUpInside];
[postView setBackgroundImage:[UIImage imageNamed:@"bbi_texture.png"] forState:UIControlStateNormal];
[postView setBackgroundImage:[UIImage imageNamed:@"bbi_texture.png"] forState:UIControlStateHighlighted];

UIBarButtonItem *postButton = [[UIBarButtonItem alloc] initWithCustomView:postView];
[self.navigationItem setRightBarButtonItem:postButton];
于 2013-05-26T04:05:31.923 回答