我已经使用自定义 UIButton 初始化了 UIBarButtonItem,并将其设置为导航 Controller 中的 rightBarButtonItem。
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightNavigationButton];//rightNavigationButton is the custom UIButton I set up before
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
通过 UIButton 的外观代理,我设置了 UIControlStates 的颜色(正常/禁用/突出显示)。 UIBarButtonItem 的行为符合预期。
当按下“保存按钮”时,我想突出显示 UIBarButtonItem 以提供已保存内容的视觉反馈。 我想做的是通过在保存动画期间更改标题的颜色来模拟 UIBarButtonItem 的突出显示,因为它似乎不能以编程方式触发突出显示动画(或设置选定的属性)(also mentioned in this post )
所以我所做的是设置一个 IBOutlet 属性,将其连接到 InterfaceBuilder 中的 UIBarButtonItem 并将 rightBarButtonItem 分配给它。当按下“保存按钮”时,我正在尝试这样做:
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationCurveEaseOut
animations:^{
//some other animation code here
[self.barButtonItemOutlet setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:FONT_ICON size:FONTSIZE],UITextAttributeFont,
HIGHLIGHT_COLOUR,UITextAttributeTextColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 0)],UITextAttributeTextShadowOffset,
[UIColor colorWithRed:0 green:0 blue:0 alpha:0.0],UITextAttributeTextShadowColor,nil]
forState:UIControlStateNormal];
}
completion:nil
];
但是什么也没有发生。我让它以某种方式工作的唯一方法是禁用/启用它。但是由于禁用和突出显示的着色应该不同,而且我需要这两种状态,所以这不是一个真正的选择。
感谢任何帮助!
最佳答案
嗯,docs UIView 类说:UIView 类的以下属性是可动画的:
@property frame
@property bounds
@property center
@property transform
@property alpha
@property backgroundColor
@property contentStretch
所以颜色不能动画,抱歉。
编辑:我的回答涉及 UIView
动画,但也有更灵活的 CALayer
动画。
我找到了 this link ,如果您使用 CALayer
动画,也许您还可以为颜色设置动画。让我看看。
编辑:
好吧,我找到了this very useful book chapter关于核心动画。不幸的是,它似乎无法解决您的问题,因为 UIBarButtonItem
的标题颜色似乎不是可以设置动画的 CALayer
的属性。对不起,我放弃了……
关于ios - UIBarButtonItem:在动画期间更改标题字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16342067/