我制作了一个这样的ActionSheet:
现在,我想更改颜色Cancel
和Delete
,以使其具有disabled
的感觉。
我可以更改所有按钮的颜色,但不能更改单个按钮的颜色。
我尝试使用类别,但是我一直在获取warining
,ActionSheet
是Depreciated
。而且代码也不起作用。
我写了这段代码:
- (void)setButton:(NSInteger)buttonIndex toState:(BOOL)enabled {
for (UIView* view in self.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
if (buttonIndex == 0) {
if ([view respondsToSelector:@selector(setEnabled:)])
{
UIButton* button = (UIButton*)view;
button.enabled = enabled;
}
}
buttonIndex--;
}
}
}
是否有可能使通过
Action sheet
呈现的AlertViewController
的某些按钮显示为禁用状态。以下是我提供操作表的代码:
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:@"Using the alert controller" preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet.view setTintColor:[UIColor redColor]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Cancel button tappped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Distructive button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Group 1" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Distructive button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
最佳答案
您可以做的是使用setEnabled
的UIAlertAction
属性。
这将使您的按钮显示为“灰色”。
我已经修改了一部分代码以合并此更改:
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
[action setEnabled:NO];
[actionSheet addAction:action];
当然,禁用后,您无法单击按钮。
其他选项将涉及使用
UIAlertActionStyle
。有三种:
UIAlertActionStyleDefault,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
UIAlertActionStyleDefault
将使用tintColor
呈现您的按钮。UIAlertActionStyleCancel
将在页面底部呈现您的按钮。UIAlertActionStyleDestructive
用“红色”颜色呈现按钮。 (认为:“删除”按钮)
关于ios - 如何禁用通过UIAlertViewController添加的操作表上的按钮操作-(iOS 9/10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41675500/