简体   繁体   English

自定义UIBarButtonItem

[英]Custom UIBarButtonItem

I am trying to create a custom UIBarbuttonItem that uses just a png with transparency so that I only have an icon as button. 我正在尝试创建一个仅使用具有透明性的png的自定义UIBarbuttonItem,以便仅将图标作为按钮。 When I try to set the button image, set the background as white, and set the style to Plain I still get an inner shadow and black border around it. 当我尝试设置按钮图像时,将背景设置为白色,并将样式设置为“纯色”时,它周围仍带有内部阴影和黑色边框。

What gives? 是什么赋予了?

在此处输入图片说明

I have tried the below code and it still puts the black border around it. 我尝试了以下代码,但仍然在其周围放置了黑色边框。

UIImage *background = [UIImage imageNamed:@"Dismiss_normal.png"];
UIImage *backgroundSelected = [UIImage imageNamed:@"Dismiss_selected.png"];
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.closeButton addTarget:self action:@selector(closeButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[self.closeButton setBackgroundImage:background forState:UIControlStateNormal];
[self.closeButton setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
self.closeButton.frame = CGRectMake(0 ,0,background.size.width, background.size.height);
self.closeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.closeButton];
self.navigationItem.leftBarButtonItem = self.closeButtonItem;

What I noticed is if I do a modal segue the button, with the code above still has a black border around it, but if I do a push segue it doesn't? 我注意到的是,如果我对按钮进行了模态选择,上面的代码仍带有黑色边框,但是如果我进行了按钮选择,则没有? WTF? WTF?

You must set the button type to Custom and the icon image to the button background . 您必须将按钮类型设置为“ 自定义” ,并将图标图像设置为按钮背景 example code: 示例代码:

UIImage *background = [UIImage imageNamed:@"icon.png"];
UIImage *backgroundSelected = [UIImage imageNamed:@"icon_selected.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[button setBackgroundImage:background forState:UIControlStateNormal];
[button setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
button.frame = CGRectMake(0 ,0,35,35);

Then set this button to your BarButtonItem like this: 然后按如下所示将此按钮设置为BarButtonItem:

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;

I understood your problem. 我了解您的问题。

When you actually push a UIViewController it will be a part of existing UINavigationController . 当您实际推送UIViewController ,它将成为现有UINavigationController的一部分。 So the UINavigationBar retains. 因此保留了UINavigationBar But when you use ModalViewController its just another UIViewController kind of "added" to your existing UIViewController . 但是,当您使用ModalViewController它只是将另一种UIViewController “添加”到现有的UIViewController So the UINavigationBar won't be there. 因此, UINavigationBar将不存在。

But you can add another UINavigationBar from xib and add a UIButton . 但是您可以从xib添加另一个UINavigationBar并添加UIButton But don't directly add the UIButton , if you do so it will automatically convert to UIBarButton making it bordered. 但是不要直接添加UIButton ,如果这样做,它将自动转换为UIBarButton使其成为边框。

So first drag a normal UIButton to your UIView (not navigation bar). 因此,首先将普通的UIButton拖到您的UIView (而不是导航栏)上。 Change it to custom and add UIImage to it using Attribute Inspector . 将其更改为custom并使用Attribute InspectorUIImage添加到其中。 Then drag that image to your custom created UINavigationBar . 然后将该图像拖动到您自定义创建的UINavigationBar It will work as I tried it just now. 它会像我刚才尝试的那样工作。 You can see the screen shot attached below. 您可以在下面看到屏幕截图。

在此处输入图片说明

This will update ALL back buttons and navigation bars throughout the app. 这将更新整个应用程序中的所有后退按钮和导航栏。

I have used the Following code to set Custom Navigation back button 我已使用以下代码设置“自定义导航”后退按钮

UIImage *imgBack = [[UIImage imageNamed:@"nav_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(-6, 22, -6, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:imgBack forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

and the Following code for Setting background image on navigation bar. 以及以下用于在导航栏上设置背景图像的代码。

UIImage *navBackgroundImage = [UIImage imageNamed:@"nav_bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

Try use [UIBarButtonItem initWithCustomView] 尝试使用[UIBarButtonItem initWithCustomView]

UIImage *menuButtonImage = [UIImage imageNamed:@"list.png"];// set your image Name here 
UIButton *btnToggle = [UIButton buttonWithType:UIButtonTypeCustom];
[btnToggle setImage:menuButtonImage forState:UIControlStateNormal];
btnToggle.frame = CGRectMake(0, 0, menuButtonImage.size.width, menuButtonImage.size.height);
UIBarButtonItem *menuBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnToggle];
self.navigationItem.leftBarButtonItem = menuBarButton;

I personally like FlatUIKit's approach by just flattening out the entire UI through UIAppearance 我个人喜欢FlatUIKit的方法,就是通过UIAppearance展平整个UI

Essentially, you'd do something like: 本质上,您将执行以下操作:

UIImage *backButtonPortraitImage = [UIImage backButtonImageWithColor:color barMetrics:UIBarMetricsDefault cornerRadius:cornerRadius];
UIImage *highlightedBackButtonPortraitImage = [UIImage backButtonImageWithColor:highlightedColor barMetrics:UIBarMetricsDefault cornerRadius:cornerRadius];

[appearance setBackButtonBackgroundImage:backButtonPortraitImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[appearance setBackButtonBackgroundImage:highlightedBackButtonPortraitImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

UIImage *buttonImageNormal       = [UIImage imageWithColor:color cornerRadius:cornerRadius];
UIImage *buttonImageHightlighted = [UIImage imageWithColor:highlightedColor cornerRadius:cornerRadius];

[appearance setBackgroundImage:buttonImageNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[appearance setBackgroundImage:buttonImageHightlighted forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

Try this code.. 试试这个代码。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *btnImage = [UIImage imageNamed:@"bg"];
[button setImage:btnImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, btnImage.size.width, btnImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIImage *icon = [UIImage imageNamed:@"icon"]
UIImageView *iconView = [[UIImageView alloc] initWithImage:icon];
iconView.frame = CGRectMake(0, 0, icon.size.width, icon.size.height);
[button addSubview:iconView];
iconView..userInteractionEnabled=NO;
[iconView centerBothDirections];
[iconView release];
UIBarButtonItem *barbtn = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem =barbtn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM