ios - 自定义按钮 initWithImage 在自定义图像后面显示默认按钮

标签 ios xcode uibutton

我正在尝试使用自定义图像为我的导航栏制作一个按钮。当我运行以下代码时,该按钮会正常显示,只是您可以在它后面看到另一个更宽的按钮,突出两侧。我如何摆脱其他按钮?

UIBarButtonItem *emailButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"emailBut"]
                                                                style:UIBarButtonSystemItemCompose
                                                               target:self 
                                                               action:@selector(emailSheet)];
self.navigationItem.rightBarButtonItem = emailButton;

最佳答案

嗯... UIBarButtonSystemItemCompose 是一个系统图标,所以我猜你可能将你的图标覆盖在它上面。您应该为您的 style: 参数使用 UIBarButtonItemStylePlain(或其他样式)。

编辑:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(...)];
[button setBackgroundImage:someImage];
[button addTarget:self action:@selector(something:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barButtonItem];

关于ios - 自定义按钮 initWithImage 在自定义图像后面显示默认按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11837479/

相关文章:

ios - 我的 iOS 框架中的异常显示私有(private)代码?

ios - Parse.com iOS localDatastore 仅固定,启用后不查找对象。 (也无法取消固定,因为它也找不到对象)

ios - Xcode 7 beta 4 构建错误 - com.apple.CoreSimulator.SimRuntime.iOS-9-0

iphone - 为东部时区的 future 日期设置本地通知

带有右导航箭头的 iOS 联系人样式按钮

ios - 使用多个图像创建自定义 UIButton 并移动 Label 并可能添加属性

ios - 我如何知道用户是否点击了按钮?

ios - 如何在实时lldb控制台上打印UIView.transform

ios - 在 iOS 中获取子模块项目的 crashlytics 日志

xcode - Xcode 8警告:没有规则可以处理架构x86_64的net.daringfireball.markdown类型的文件