您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 技巧大全

UIActionSheet自訂高度的三種方案比較與完整實作

編輯:iOS 技巧大全

本文整理UIActionSheet在自訂高度與樣式上的幾種做法,並附上重新實作版本的完整程式碼,供iOS開發者參考。

最近專案需要用到從底部彈出的選擇框,看了前輩寫的程式碼、也搜尋了網路上的做法,發現其實都沒有把問題真正解決好。以下把自己的研究結果整理出來分享,若有不足之處歡迎指教。

最古老的方法一

-(void)CreateActionSheet{

self.actionSheet= [[UIActionSheet alloc] initWithTitle:@"選擇" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"a",@"a",@"a",nil];

[self.actionSheet addSubview:self.baseView];

}

這個做法是在系統的ActionSheet圖層上再覆蓋一層自訂視圖,以達到底部彈出的效果。

優點:使用簡單方便。

缺點:無法很好地解決ActionSheet的高度問題,高度無法自訂。

改進方法二

繼承UIActionSheet並覆寫 -(void)layoutSubviews 方法。網路上這類程式碼很多,可以自行參考。

優點:實現了ActionSheet的高度自訂。

缺點:失去了原有ActionSheet的動畫效果與陰影點擊效果。

較完整的方案三

方案一:在方法二的基礎上補回動畫與陰影點擊效果。問題可以解決得很好,但需要對iOS 6與iOS 7分別處理,因為iOS 7時ActionSheet的父視圖有所改變,有興趣的可以實際檢視看看。

方案二:依照iOS 7中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就完成了。你可以在這個基礎上繪製自己需要的介面、加上自己的delegate,讓它符合專案的設計規範。

實作上有幾個細節值得注意:一是 show 方法中掛載用的 mainWindow 目前是註解狀態,實際使用時需自行取得合適的window或改用addSubview到指定的容器視圖;二是動畫採用frame位移加遮罩透明度變化,若專案支援多個window或分屏,需另外處理safeArea的對齊問題。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved