iPhone:设置navigationItem按钮的宽度属性
我一直在尝试绕过设置 navigationitem 按钮的 width 属性,但找不到一种方法来完成我正在寻找的任务。
以下代码可以很好地设置我想要的按钮大小并且看起来很好,但我没有看到实际的按钮,它只是我的图像。它应该看起来像按钮并且有背景图像!
UIButton *addCommentButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addCommentButton setFrame:CGRectMake(0, 0, 25, 25)];
[addCommentButton addTarget:self action:@selector(addComment) forControlEvents:UIControlEventTouchUpInside];
[addCommentButton setBackgroundImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addCommentButton];
以下代码可以做到这一点,但我无法设置按钮大小(25,25)!
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myImage.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(addComment)];
我尝试明确设置宽度,但它没有任何区别!
[[self.navigationItem rightBarButtonItem] setWidth] = 25;
有人可以帮我解决这个问题吗?谢谢。
I have been trying to get around setting navigationitem button's width property but couldn't find a way to get it done what exactly I am looking for.
Following code works fine setting button size what I want and appear nicely but I don't see actually button, its rather only my image. It should appear like button and it has background image!
UIButton *addCommentButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addCommentButton setFrame:CGRectMake(0, 0, 25, 25)];
[addCommentButton addTarget:self action:@selector(addComment) forControlEvents:UIControlEventTouchUpInside];
[addCommentButton setBackgroundImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addCommentButton];
Following code does that, but I can't set button size (25,25)!!
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myImage.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(addComment)];
I tried setting width explicitly but it does make no difference!
[[self.navigationItem rightBarButtonItem] setWidth] = 25;
Can someone please help me to sort out this issue. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有快速解释来解决您的问题,但我认为以下链接中发布的代码将帮助您解决问题。
UINavigationBar 和后退按钮的自定义
I don't have a quick explanation to fix your issue, but I think the code posted in the following link will help you solve it.
Customization of UINavigationBar and the back button
您的第一个代码片段会生成一个自定义按钮,该按钮不会具有“按钮”的外观和感觉。第二个代码片段提供了一个带有边框的 UIBarButtonItem 样式(不是 UIButton)的 UIBarButtonItem。使用第一个代码片段,只需创建一个按钮类型为 RoundedRect 而不是 Custom 的 UIButton。
Your first code snippet generates a custom button, which won't have a "button" look and feel. The second code snippet gives a UIBarButtonItem with a bordered UIBarButtonItem style (not a UIButton). Using the first code snippet, just create a UIButton with a button type RoundedRect instead of Custom.