最近程序中用到了,底部彈出框,看了下,前輩寫得代碼,搜索了下網路,發現其實都沒有很好的解決問題,所以研究了下,將代碼分享出來,跟大家分享一下,有問題的話,歡迎各位大牛批評指正。
-(void)CreateActionSheet{
self.actionSheet= [[UIActionSheetalloc]initWithTitle:@"選擇"delegate:selfcancelButtonTitle:@"cancel"destructiveButtonTitle:nilotherButtonTitles:@"a",@"a",@"a",nil];
[self.actionSheetaddSubview:self.baseView];
}
上面的方法,是在系統的ActionSheet圖層上覆蓋圖層,已達到底部彈出的效果。
優點:使用簡單方便
缺點: 這個方法無法很好的解決ActionSheet高度問題,沒有辦法自定
繼承UIActionSheet 重寫-(void)layoutSubviews方法。網上有很多這種代碼,大家可以去看下。
優點:實現了ActionSheet的高度自定問題
缺點:失去了原有ActionSheet中得動畫效果,和陰影點擊效果
方案一: 在方法二中,補全確實的動畫效果和陰影點擊效果。問題可以很好的解決,單需要對IOS6和IOS7做不同的處理,因為IOS7時,ActonSheet的父視圖有所改變,有興趣的童鞋可以看看。
想要代碼的可以給我留言。
方案二:按照IOS7中ActionSheet的原理重新定制。話不多說直接上代碼
#import <UIKit/UIKit.h> @interface RecreateActionSheet : UIView @property (nonatomic,retain) UIButton * baseView; @property (nonatomic,retain) UIView * baseActionSheet; @property (nonatomic,retain) UIView * contentView; @property (nonatomic,retain) UINavigationBar * navBar; @property (nonatomic,retain) UINavigationItem * navItem; -(id)initWithHeight:(CGFloat)_height WithTitle:(NSString *)_title; -(void)show; -(void)dismiss; @end
#import "RecreateActionSheet.h" @interface RecreateActionSheet (){ CGRect mainBounds; //主屏幕大小 // UIWindow * mainWindow; //主屏幕 CGRect actionRect1; //初始位置 CGRect actionRect2; //結束位置 } @end @implementation RecreateActionSheet -(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } -(id)initWithHeight:(CGFloat)_height WithTitle:(NSString *)_title{ mainBounds = [UIScreen mainScreen].bounds; // mainWindow = [[UIApplication sharedApplication].windows objectAtIndex:0]; actionRect1 = CGRectMake(0, mainBounds.size.height, mainBounds.size.width, 0); actionRect2 = CGRectMake(0, mainBounds.size.height - _height - 44, mainBounds.size.width, _height + 44); self = [super initWithFrame:mainBounds]; if (self) { // Initialization code [self createUiWithHeight:_height]; [self createNavItemWithTitle:_title]; } return self; } -(void)dealloc{ [self.baseActionSheet release]; [self.contentView release]; [self.navBar release]; [self.navItem release]; [super dealloc]; } -(void)createUiWithHeight:(CGFloat)_height{ _baseView = [UIButton buttonWithType:UIButtonTypeCustom]; _baseView.frame = mainBounds; _baseView.backgroundColor = [UIColor blackColor]; _baseView.alpha = 0; [_baseView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_baseView]; _baseActionSheet = [[UIView alloc] init]; _baseActionSheet.frame = actionRect1; _baseActionSheet.backgroundColor = [UIColor clearColor]; [self addSubview:_baseActionSheet]; _navBar = [[UINavigationBar alloc] init]; _navBar.frame = CGRectMake(0, 0, mainBounds.size.width, 44); _navBar.barStyle = UIBarStyleBlackOpaque; [_baseActionSheet addSubview:_navBar]; _contentView = [[UIView alloc] init]; _contentView.frame = CGRectMake(0, 44, mainBounds.size.width, _height); _contentView.backgroundColor = [UIColor whiteColor]; [_baseActionSheet addSubview:_contentView]; } -(void)createNavItemWithTitle:(NSString *)_title{ _navItem = [[UINavigationItem alloc] initWithTitle:nil]; UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonCancel)]; UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithTitle:@"確定" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonOk)]; _navItem.leftBarButtonItem = leftButton; _navItem.rightBarButtonItem = rightButton; _navItem.title = _title; [leftButton release]; [rightButton release]; [self.navBar setItems:[NSArray arrayWithObject:_navItem]]; } #pragma mark 取消和確定按鈕事件 -(void)buttonOk{ [self dismiss]; } -(void)buttonCancel{ [self dismiss]; } #pragma mark 顯示和隱藏 -(void)show{ [mainWindow addSubview:self]; [self addAnimationIn]; } -(void)dismiss{ [self addAnimationOut]; } #pragma mark 顯示和隱藏動畫 -(void)addAnimationIn{ [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^(void){ self.baseActionSheet.frame = actionRect2; self.baseView.alpha = 0.4; } completion:^(BOOL finished){}]; } -(void)addAnimationOut{ [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^(void){ self.baseActionSheet.frame = actionRect1; self.baseView.alpha = 0; } completion:^(BOOL finished){[self removeFromSuperview];}]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
這樣一個完美ActionSheet就產生了,童鞋們可以在這個基礎上,繪制自己需要的界面,添加自己的委托等等。