IOS開發UI篇之──自定義UIActionSheet【轉】
阿新 • • 發佈:2019-01-28
UIActionSheet類系iOS開發中實現警告框的重要的類,而在好多應用中,都對它進行了擴充套件,今天介紹一下自定義風格的UIActionSheet
一、自定義CustomActionSheet類
CustomActionSheet類繼承UIActionSheet,具體的實現如下所示:
1)CustomActionSheet.h標頭檔案
二、利用自定義的CustomActionSheet類顯示提示框#import <Foundation/Foundation.h> @interface CustomActionSheet : UIActionSheet { UIToolbar* toolBar; UIView* view; } @property(nonatomic,retain)UIView* view; @property(nonatomic,retain)UIToolbar* toolBar; /*因為是通過給ActionSheet 加 Button來改變ActionSheet, 所以大小要與actionsheet的button數有關 *height = 84, 134, 184, 234, 284, 334, 384, 434, 484 *如果要用self.view = anotherview. 那麼another的大小也必須與view的大小一樣 */ -(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title; @end 2)CustomActionSheet.m實現檔案 #import "CustomActionSheet.h" @implementation CustomActionSheet @synthesize view; @synthesize toolBar; -(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title { self = [super init]; if (self) { int theight = height - 40; int btnnum = theight/50; for(int i=0; i<btnnum; i++) { [self addButtonWithTitle:@" "]; } toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; toolBar.barStyle = UIBarStyleBlackOpaque; UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:nil action:nil]; UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(docancel)]; UIBarButtonItem *fixedButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; NSArray *array = [[NSArray alloc] initWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil]; [toolBar setItems: array]; [titleButton release]; [leftButton release]; [rightButton release]; [fixedButton release]; [array release]; [self addSubview:toolBar]; view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)]; view.backgroundColor = [UIColor groupTableViewBackgroundColor]; [self addSubview:view]; } return self; } -(void)done { [self dismissWithClickedButtonIndex:0 animated:YES]; } -(void)docancel { [self dismissWithClickedButtonIndex:0 animated:YES]; } -(void)dealloc { [view release]; [super dealloc]; } @end
#import "TestActionSheetViewController.h" #import "CustomActionSheet.h" @implementation TestActionSheetViewController -(IBAction)btndown { CustomActionSheet* sheet = [[CustomActionSheet alloc] initWithHeight:284.0f WithSheetTitle:@"自定義ActionSheet"]; UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,50, 320, 50)]; label.text = @"這裡是要自定義放的控制"; label.backgroundColor = [UIColor clearColor]; label.textAlignment = UITextAlignmentCenter; [sheet.view addSubview:label]; [sheet showInView:self.view]; [sheet release]; } @end
這裡的UILabel是作一個示例,在這個位置你可以換成你自己的內容即可;
三、效果圖