pod 'MMPopupView'
一 自定義界面
1 類型解析
MMPopupType
是MMPopupView
的一個枚舉變量,主要用于動畫效果
-
MMPopupTypeCustom
: 顯示的時候從屏幕頂部移動到屏幕中央,消失的時候從屏幕中央移出屏幕底部 -
MMPopupTypeAlert
: 類似于UIAlertView的動畫效果,顯示的時候有個從屏幕中央放大的效果,消失的時候無動畫 -
MMPopupTypeSheet
: 類似于UIActionSheet的動畫,顯示的時候從底部彈起,消失的時候縮回底部
2 區別
MMPopupTypeCustom
和MMPopupTypeAlert
會自動把自定義界面放在屏幕中央,而設置為MMPopupTypeSheet
的時候則界面底部會保持在屏幕底部的位置
3 自定義
#import <MMPopupView/MMPopupView.h>
@interface TestView : MMPopupView
@implementation TestView
- (instancetype)init
{
if (self = [super init]) {
//點擊半透明背景使界面自動消失
[MMPopupWindow sharedWindow].touchWildToHide = YES;
//設置類型
self.type = MMPopupTypeCustom;
//設置尺寸,self只需設置寬高,會根據類型來確定在屏幕中的位置
#//請使用Masonry相關方法來設置寬高,否則會有問題
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(200);
make.height.mas_equalTo(200);
}];
self.backgroundColor = WHITE_COLOR;
//使用[self addSubview:subview];來添加其他控件
}
return self;
}
4 使用
TestView *popupView = [[TestView alloc] init];
[popupView show];
5 注意點
在設置self
尺寸的時候,如果使用self.size = CGSizeMake(w,h)
方式來設置,會出現尺寸及相關界面紊亂問題,由于在MMPopupView
內部的尺寸變化是通過Masonry
相關方法來實現的,所以在設置self
尺寸的時候盡量使用Masonry
相關方法,不過subViews
可以使用CGRectMake
這種方式來設置尺寸。
二 使用MMSheetView
1 首先需要引入相關文件
#import <MMPopupView/MMSheetView.h>
2 基本使用
MMPopupItemHandler block = ^(NSInteger index) {
//添加的第一個元素索引為0,依次增加
};
NSArray *items =
@[MMItemMake(@"item1", MMItemTypeHighlight, block),
MMItemMake(@"item2", MMItemTypeNormal, block),
MMItemMake(@"item3", MMItemTypeDisabled, block)];
MMSheetView *view = [[MMSheetView alloc] initWithTitle:@"title" items:items];
// view.attachedView = self.view;
[view show];
3 關于attachedView
attachedView
意思就是依附的View
,被依附的View
所有交互不可用,其他View
的交互保持不變,如果不設置這個屬性,則屏幕內所有的交互不可用,因為作者的文檔是這么寫的// default is MMPopupWindow. You can attach MMPopupView to any UIView.
,MMPopupWindow
是一個全屏的Window
。
4 配置相關屬性
我們發現MMSheetView
顯示的時候底部的按鈕顯示為取消
,然而在上面的代碼中我們并沒有配置這個屬性,這是因為取消
是默認的,如果想修改這個字段或者相關的其他屬性,可以使用下面的代碼,這些代碼最好放在初始化MMSheetView
的代碼之前
[MMSheetViewConfig globalConfig].defaultTextCancel = @"Cancel";
三 AlertView
1 首先引入相關文件
#import <MMPopupView/MMAlertView.h>
2.1 最常見的使用
MMPopupItemHandler block = ^(NSInteger index) {
//添加的第一個元素索引為0,一次增加
};
NSArray *items =
@[MMItemMake(@"確定", MMItemTypeHighlight, block),
MMItemMake(@"取消", MMItemTypeNormal, block)];
MMAlertView *view = [[MMAlertView alloc] initWithTitle:@"Title" detail:@"Detail" items:items];
[view show];
2.2 只帶一個按鈕的提示框
一般用于信息的展示,用戶看完后點擊按鈕消失,沒有監聽事件的產生
MMAlertView *view = [[MMAlertView alloc] initWithConfirmTitle:@"OK" detail:@"Detail"];
[view show];
2.3 帶輸入框的提示框
提示框自帶一個取消
和一個確定
按鈕
- 取消按鈕負責關閉提示框
- 確定按鈕負責輸入信息的提?。喊聪麓_定按鈕,會觸發handler對應的block
- 如果text長度不為0,則會關閉提示框
- 如果text長度為0,則提示框依舊存在
MMAlertView *view = [[MMAlertView alloc] initWithInputTitle:@"Title" detail:@"Detail" placeholder:@"Placeholder" handler:^(NSString *text) {
NSLog(@"text %@", text);
}];
[view show];
3 相關屬性配置
使用MMAlertViewConfig
來配置相關的屬性,用法和MMSheetViewConfig
一樣