如何使 NavigationItem UIBarButtonItems 在视觉上确认触摸?

发布于 2024-09-18 07:11:01 字数 569 浏览 11 评论 0原文

我有一个 UINavigationController,我在其中初始化导航栏(使用 MonoTouch):

NavigationBar.BarStyle = UIBarStyle.Black;
NavigationBar.TintColor = UIColor.Black;

在随后推送到导航控制器上的 UIViewController 上,我添加一个按钮:

NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Add, (s, e) => HandleAddItem());

但是,当我触摸按钮时,它不会改变颜色/阴影(即动画)表示它已被触摸。类似的 UIBarButtonItems 添加到另一个视图控制器(普通 UIVIewController)上手动创建的 UINavigationBar 中,动画如我所料。

当导航栏按钮位于已推送到 UINavigationController 上的 UIViewController 上时,如何使导航栏按钮“闪烁”?

I have a UINavigationController where I initialize the NavigationBar as such (using MonoTouch):

NavigationBar.BarStyle = UIBarStyle.Black;
NavigationBar.TintColor = UIColor.Black;

On a UIViewController that I subsequently push onto the navigation controller, I add a button as such:

NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Add, (s, e) => HandleAddItem());

However, when I touch the button, it doesn't change color/shade (i.e. animate) to signify it's been touched. Similar UIBarButtonItems added to a manually-created UINavigationBar on another view controller (plain UIVIewController) animate as I'd expect.

How can I get the navigation bar buttons to "flash" when they are on a UIViewController that has been pushed onto a UINavigationController?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤星 2024-09-25 07:11:01

我发现,一旦更改 UINavigationBar 的 barStyletintColor,按钮的突出显示状态很可能与默认状态没有什么不同。我相信解决此问题的最佳方法是使用从自定义视图创建的 UIBarButtonItem。

UIImage *bgImage = [UIImage imageNamed:@"button-normal.png"];
bgImage = [bgImage stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];
UIImage *bgImageHighlighted = [UIImage imageNamed:@"button-highlighted.png"];
bgImageHighlighted = [bgImageHighlighted stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImageHighlighted forState:UIControlStateHighlighted];

UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];

这有点痛苦,尤其是当您的按钮使用图像而不是标题时。我绝对建议向 Apple 提交有关此问题的错误报告。

在您的特定情况下,仅将 barStyle 设置为 UIBarStyleBlack 并保留 tintColor 属性可能会更好。我也很幸运,只指定 tintColor 并将 barStyle 设置为 UIBarStyleDefault,只要 tintColor 是不黑。一般来说,黑色的 tintColor 效果不太好。

I have found that once you change the UINavigationBar's barStyle and tintColor, there is a very good chance that the highlighted state for the button will be no different than the default state. I believe the best way to work around this is to use a UIBarButtonItem created from a custom view.

UIImage *bgImage = [UIImage imageNamed:@"button-normal.png"];
bgImage = [bgImage stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];
UIImage *bgImageHighlighted = [UIImage imageNamed:@"button-highlighted.png"];
bgImageHighlighted = [bgImageHighlighted stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImageHighlighted forState:UIControlStateHighlighted];

UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];

It's kind of a pain, especially when your button uses an image instead of a title. I would definitely recommend submitting a bug report to Apple about this.

In your particular situation, it might work better to only set the barStyle to UIBarStyleBlack and leave the tintColor property alone. I have also had luck only specifying the tintColor and leaving barStyle set to UIBarStyleDefault, as long as the tintColor is not black. In general, a tintColor of black does not work very well.

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