是否有一种简单的方法可以为UIBarButtonItems打开状态和关闭状态,每种状态都有不同的图像?
由于
答案 0 :(得分:2)
没有内置方式,但我可以考虑一些方法(取决于您的需求):
image
属性创建自己的UIBarButtonItem
子类,如下所示:
@interface ToggleBarButtonItem : UIBarButtonItem {
BOOL _state;
UIImage * onImage;
UIImage * offImage;
}
- (BOOL)toggleState;
@property (nonatomic, retain) UIImage * onImage;
@property (nonatomic, retain) UIImage * offImage;
@end
@implementation ToggleBarButtonItem
- (BOOL)toggleState {
if (_state) {
// Switch to Off state
self.image = offImage;
}
else {
// Switch to On state
self.image = onImage;
}
return _state = !_state;
}
@end