具有许多项目的 UIActionSheet 覆盖文本
Posted
技术标签:
【中文标题】具有许多项目的 UIActionSheet 覆盖文本【英文标题】:UIActionSheet with many items overlays text 【发布时间】:2014-05-21 19:54:07 【问题描述】:我们有一个包含许多选项的 UIActionSheet。在 iPad 上的 ios 7 中,滚动列表时,它会显示原始值。这是 iPad 7 的错误还是我们可以做出改变? iOS 6 iPad 和 iPhone 在我们看来还不错。
//
// ViewController.m
// AlertViewTest
//
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_data = [NSMutableArray new];
[_data addObject:@"Arthur"];
[_data addObject:@"Bertha"];
[_data addObject:@"Cristobal"];
[_data addObject:@"Dolly"];
[_data addObject:@"Edouard"];
[_data addObject:@"Fay"];
[_data addObject:@"Gonzalo"];
[_data addObject:@"Hanna"];
[_data addObject:@"Isaias"];
[_data addObject:@"Josephine"];
[_data addObject:@"Kyle"];
[_data addObject:@"Laura"];
[_data addObject:@"Marco"];
[_data addObject:@"Nana"];
[_data addObject:@"Omar"];
[_data addObject:@"Paulette"];
[_data addObject:@"Rene"];
[_data addObject:@"Sally"];
[_data addObject:@"Teddy"];
[_data addObject:@"Vicky"];
[_data addObject:@"Wilfred"];
[_data addObject:@"Ana"];
[_data addObject:@"Bill"];
[_data addObject:@"Claudette"];
[_data addObject:@"Danny"];
[_data addObject:@"Erika"];
[_data addObject:@"Fred"];
[_data addObject:@"Grace"];
[_data addObject:@"Henri"];
[_data addObject:@"Ida"];
[_data addObject:@"Joaquin"];
[_data addObject:@"Kate"];
[_data addObject:@"Larry"];
[_data addObject:@"Mindy"];
[_data addObject:@"Nicholas"];
[_data addObject:@"Odette"];
[_data addObject:@"Peter"];
[_data addObject:@"Rose"];
[_data addObject:@"Sam"];
[_data addObject:@"Teresa"];
[_data addObject:@"Victor"];
[_data addObject:@"Wanda"];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (IBAction)showAlert:(id)sender
if([self.actionSheet isVisible])
[self.actionSheet dismissWithClickedButtonIndex:[self.actionSheet cancelButtonIndex] animated:YES];
self.actionSheet.delegate = nil;
self.actionSheet = nil;
return;
if(nil == self.actionSheet)
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select one"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
if(![self shouldPresentActionSheet:self.actionSheet])
self.actionSheet = nil;
return;
[self.actionSheet setCancelButtonIndex:[self.actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"Cancel")]];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
[self.actionSheet showFromBarButtonItem:self.button animated:YES];
else
[self.actionSheet showFromToolbar:self.toolbar];
-(BOOL)shouldPresentActionSheet:(UIActionSheet *)actionSheet
if(actionSheet == self.actionSheet)
if([self.data count] == 0)
return NO;
for(NSString *d in self.data)
[self.actionSheet addButtonWithTitle:d];
return YES;
@end
【问题讨论】:
考虑使用另一个界面(弹出框中的表格视图)。操作表从来都不是这样敲的,实际上您只是将其用作懒人的表格视图。 我会说你在滥用 UIActionSheet。它适用于用户可以选择的不超过 10 个操作项。当然,这不是一个硬性数字,但在操作表中显示整个列表并不是一个好主意。我会创建一个单独的视图控制器,其中包含一个表格视图(本质上就是一个操作表),让用户进行选择。 发帖后,我差点编辑评论,以保护我的信誉。通常这将是 2-4 个项目,但我们有一个客户以一种奇怪的方式使用它。我发布的只是示例代码,以展示我认为是错误的内容。 查看link 了解解决方法 我可能是错的,但这似乎在 iOS 8 中已修复 【参考方案1】:cmets 中的@Flores 是对的,以下是解决此问题的方法:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
actionSheet.backgroundColor = [UIColor whiteColor];
for (UIView *subview in actionSheet.subviews)
subview.backgroundColor = [UIColor whiteColor];
【讨论】:
【参考方案2】:应该是这样的
//view did load stuff
- (IBAction)showAlert:(id)sender
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// ObjC Fast Enumeration
for (NSString *title in _data)
[actionSheet addButtonWithTitle:title];
[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = [_data count];
[actionSheet showInView:self.view];
Source
【讨论】:
我认为这不是我们正在做的事情。以上是关于具有许多项目的 UIActionSheet 覆盖文本的主要内容,如果未能解决你的问题,请参考以下文章
带有许多按钮的 Xcode iPad UIActionSheet 无法正确显示 iOS7
更改 UIActionSheet 中项目的文本颜色 - iOS 8