iPhone:设置navigationItem按钮的宽度属性

发布于 2025-01-02 18:14:08 字数 992 浏览 2 评论 0原文

我一直在尝试绕过设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2025-01-09 18:14:08

我没有快速解释来解决您的问题,但我认为以下链接中发布的代码将帮助您解决问题。

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

跨年 2025-01-09 18:14:08

您的第一个代码片段会生成一个自定义按钮,该按钮不会具有“按钮”的外观和感觉。第二个代码片段提供了一个带有边框的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文