是否有可能创建一个ActionSheet
,在这个图像的两个按钮之间有一个标签?
现在真的需要这个。谁能帮我?谢谢。
编辑2018年
我在iOS4中发布此代码时可能会有用。 iOS开发从那时起已经发展壮大,请不要使用此代码,除非您出于任何原因为iOS4编写应用程序!请参考精彩的第三方库XLR Action Controller,了解您的现代操作表需求!
这当然是可能的,我喜欢一直这样做!
首先,制作一个@property
的UIActionSheet
(在这个例子中,我的名字叫aac
--这很方便,特别是如果你想通过UITextFields
称之为。
接下来,在您打开动作表的方法中,比如-(IBAction)userPressedButton:(id)sender
,创建动作表的本地实例。
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
然后将其添加到您的财产:self.aac = actionsheet
现在你基本上已完全掌权在这个ActionSheet中做任何事情,我将给你一个缩写形式,我为最近的项目做了什么:
- (IBAction)sendDataButtonPressed:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
self.aac = actionSheet;
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"actionsheet_bg.png"]];
[background setFrame:CGRectMake(0, 0, 320, 320)];
background.contentMode = UIViewContentModeScaleToFill;
[self.aac addSubview:background];
UIButton *cancelButton = [UIButton buttonWithType: UIButtonTypeCustom];
cancelButton.frame = CGRectMake(0, 260, 320, 50);
[cancelButton setBackgroundImage:[UIImage imageNamed:@"actionsheet_button.png"] forState: UIControlStateNormal];
[cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
cancelButton.adjustsImageWhenHighlighted = YES;
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor colorWithRed:0/255.0f green:177/255.0f blue:148/255.0f alpha:1.0f] forState:UIControlStateNormal];
cancelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
cancelButton.titleLabel.font = [UIFont fontWithName: @"SourceSansPro-Light" size: 25];
[self.aac addSubview: cancelButton];
UIButton *emailResultsButton = [UIButton buttonWithType: UIButtonTypeCustom];
emailResultsButton.frame = CGRectMake(25, 12, 232, 25);
[emailResultsButton addTarget:self action:@selector(emailResultsTapped:) forControlEvents:UIControlEventTouchUpInside];
emailResultsButton.adjustsImageWhenHighlighted = YES;
[emailResultsButton setTitle:@"Email Results" forState:UIControlStateNormal];
[emailResultsButton setTitleColor:[UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f] forState:UIControlStateNormal];
[emailResultsButton setTitleColor:[UIColor colorWithRed:0/255.0f green:177/255.0f blue:148/255.0f alpha:1.0f] forState:UIControlStateHighlighted];
emailResultsButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
emailResultsButton.titleLabel.font = [UIFont fontWithName: @"SourceSansPro-Light" size: 20];
[self.aac addSubview: emailResultsButton];
// lots of other buttons...
// then right at the end you call the showInView method of your actionsheet, and set its counds based at how tall you need the actionsheet to be, as follows:
[self.aac showInView:self.view];
[self.aac setBounds:CGRectMake(0,0,320, 600)];
这是一张会发生什么的图片(记得我省略了代码示例中的所有其他按钮,但是这里有图片):
现在,如果要关闭actionSheet - 取决于您设置按钮结构的方式,或者使用UIToolBar(非常常见) - 您可以在按钮的选择器操作中执行以下操作:
-(void)cancelButtonClicked:(id)sender {
[self.aac dismissWithClickedButtonIndex:0 animated:YES];
}
另外,仅仅是因为你的布局所需的所有尺寸都需要一点时间,因为你没有使用UIViewController的主UIView视图,而是使用actionSheet的视图,所以它有不同的尺寸。
我做的一个技巧是使用UIView在Storyboard中布置操作表,然后将所需的所有对象放在UIView中的“actionsheet”中,并且您应该获得所有图像的准确尺寸。
如果您需要澄清,请告诉我,祝您好运!
我只是要添加一个小评论,但我不能发表评论,但是如此不好发表一个完整的答案。如果你想避免IOS7错误CGContextSetFillColorWithColor:无效的上下文0x0。这是一个严重的错误等。您可以使用以下内容。
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@""
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
然而,这会弄乱你的操作表顶部的图形/绘图,因为@Max说,如果你还没有背景你添加只是添加这个代码,为你提供默认的操作表外观。
UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
background.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1];
[self.actionSheet addSubview:background];
然后将您想要的任何其他内容添加到自定义操作表中。
如果您不想在UIActionSheet上使用任何黑客来自定义它,您可以查看我制作的UIActionSheet的替代品:https://github.com/JonasGessner/JGActionSheet它是完全可定制的!
UIButton* button = (UIButton*)sender;
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share the PNR Status" delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Send Message",@"Send Email",@"Facebook",nil];
[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"chat.png"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:2] setImage:[UIImage imageNamed:@"facebook_black.png"] forState:UIControlStateNormal];
//*********** Cancel
[[[actionSheet valueForKey:@"_buttons"] objectAtIndex:3] setImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
[actionSheet showFromRect:button.frame inView:self.view animated:YES];
如果你还在努力解决这个问题,那么我有a library可能会有所帮助。它允许您创建自定义操作表。它有一堆内置类型,也可以扩展和重新设置。