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

时间:2010-09-06 14:24:19

标签: iphone uinavigationcontroller uinavigationbar xamarin.ios uibarbuttonitem

我有一个UINavigationController,我在这里初始化NavigationBar(使用MonoTouch):

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

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

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

然而,当我触摸按钮时,它不会改变颜色/阴影(即动画)以表示它已被触摸。类似的UIBarButtonItems添加到另一个视图控制器(普通的UIVIewController)上的手动创建的UINavigationBar,就像我期望的那样动画。

如果导航栏按钮位于已推入UINavigationController的UIViewController上,如何让导航栏按钮“闪烁”?

1 个答案:

答案 0 :(得分:3)

我发现一旦你更改了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的黑色效果不佳。

相关问题