如何使用UIBarButtonItemStyleDone样式和编辑/完成按钮的编辑状态为条形按钮提供视觉上不同的条形按钮项目背景图像?UIBarButtonItem外观代理的setBackgroundImage:forState:barMetrics中没有记录的UIControlState值似乎可以解决问题.
我正在使用UIAppearance设计 UINavigationBar 的样式。我希望所有后退按钮都有灰色文本,所有 rightBarButtonItems 都有绿色文本(颜色是假设的)。由于默认情况下这两个按钮都是 UIBarButtonItems,因此 UIAppearance 无法区分这两个按钮。所以我决定对 UIBarButtonItem 进行子类化,将其命名为 ActionBarButtonItem。我在需要 rightBarButtonItem 的任何地方都使用这个新子类。
UIBarButtonItem* done = [[ActionBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStyleDone
target:self
action:@selector(onDonePress)];
self.navigationItem.rightBarButtonItem = done;
[done release];
Run Code Online (Sandbox Code Playgroud)
NSDictionary* buttonStyle = [NSDictionary
dictionaryWithObjects:[NSArray
arrayWithObjects:
[UIColor grayColor],
, nil
]
forKeys:[NSArray
arrayWithObjects:
UITextAttributeTextColor,
nil
]
];
NSDictionary* actionStyle = [NSDictionary
dictionaryWithObjects:[NSArray
arrayWithObjects:
[UIColor greenColor],
nil
]
forKeys:[NSArray
arrayWithObjects:
UITextAttributeTextColor,
nil
]
];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
setTitleTextAttributes:buttonStyle
forState:UIControlStateNormal
];
[[ActionBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
setTitleTextAttributes:actionStyle
forState:UIControlStateNormal …
Run Code Online (Sandbox Code Playgroud)