gpt4 book ai didi

objective-c - 如何自定义UIActionSheet?苹果系统

转载 作者:太空狗 更新时间:2023-10-30 03:14:09 25 4
gpt4 key购买 nike

是否可以创建一个 ActionSheet,它在两个按钮之间有一个标签,就像这张图片一样?

enter image description here

最佳答案

编辑 2018

当它可能有用时,我一直在 iOS4 中发布这段代码。从那时起,iOS 开发发展惊人,请不要使用此代码,除非您出于任何原因为 iOS4 编写应用程序!精彩3方库请引用XLR Action Controller满足您对现代操作表的需求!

这当然是可能的,而且我一直喜欢这样做!

首先,制作一个 UIActionSheet@property(在本例中,我的名称为 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)];

这是发生的情况的图片(记住我在代码示例中省略了所有其他按钮,但它们在此处显示):enter image description here

现在,如果你想关闭 actionSheet——这取决于你如何设置你的按钮结构,或者可能使用 UIToolBar(很常见)——你可以在按钮的选择器操作中执行以下操作:

-(void)cancelButtonClicked:(id)sender {
[self.aac dismissWithClickedButtonIndex:0 animated:YES];
}

此外,仅供引用,让所有尺寸都适合您的布局需要一点时间,因为您使用的不是 UIViewController 的主 UIView View ,而是 actionSheet 的 View ,因此它具有不同的维度。

我做的一个技巧是使用 UIView 在 Storyboard 中布置 Action 表,然后将您想要的所有对象放入该 UIView 的“ Action 表”中,您应该获得所有图像的准确尺寸。

如果您需要澄清,请告诉我,祝您好运!

关于objective-c - 如何自定义UIActionSheet?苹果系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17946358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com