iOS:一個(gè)簡(jiǎn)單的提示框

效果圖:


代碼:.h中

typedef void(^SBBlock)(NSInteger num);

@interface SBAlertView : UIView

/**

*? 蒙版

*/

@property (nonatomic,strong)UIView *blackView;

/**

*? 提示框

*/

@property (strong,nonatomic)UIView * alertview;

/**

*? 標(biāo)題

*/

@property (strong,nonatomic)NSString * title;

/**

*? 內(nèi)容

*/

@property (nonatomic,copy)NSString *contentStr;

-(void)initWithTitle:(NSString *)title contentStr:(NSString *)content BtnArray:(NSArray *)array andBlock:(SBBlock)block;


--------------------------------分割線--------------------------------

.m中


@interface SBAlertView ()

/**

*? 創(chuàng)建block對(duì)象

*/

@property (nonatomic, copy) SBBlock block;

/**

*? 標(biāo)題label

*/

@property(strong,nonatomic)UILabel *isCreate;

/**

*? 內(nèi)容

*/

@property(strong,nonatomic)UILabel *attenL;

/**

*? 取消按鈕

*/

@property(strong,nonatomic)UIButton *btn1;

/**

*? 確定按鈕

*/

@property(strong,nonatomic)UIButton *btn2;

/**

*? 橫線

*/

@property(strong,nonatomic)UILabel *label1;

/**

*? 豎線

*/

@property(strong,nonatomic)UILabel *label2;

@end

@implementation SBAlertView

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

//創(chuàng)建遮罩

self.blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

self.blackView.backgroundColor = [UIColor blackColor];

self.blackView.alpha = 0.5;

UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];

tap1.cancelsTouchesInView = NO;

[self.blackView addGestureRecognizer:tap1];

[self addSubview:_blackView];

//創(chuàng)建alert

self.alertview = [[UIView alloc]initWithFrame:CGRectMake(30, HEIGHT/2-120-64, WIDTH-60, 240)];

//? ? ? ? self.alertview.center = self.center;

self.alertview.layer.cornerRadius = 5;

self.alertview.clipsToBounds = YES;

self.alertview.backgroundColor = [UIColor whiteColor];

[self addSubview:_alertview];

[self createAliertView];

}

return self;

}

-(void)viewTapped:(UITapGestureRecognizer*)tap1

{

self.alpha = 0.0;

[self removeFromSuperview];

self.alertview = nil;

}

//創(chuàng)建View

-(void)createAliertView{

self.isCreate = [[UILabel alloc]initWithFrame:CGRectMake( 20,10, self.alertview.frame.size.width-40, 43)];

self.isCreate.font = [UIFont boldSystemFontOfSize:18];

self.isCreate.textAlignment=NSTextAlignmentCenter;

self.attenL = [[UILabel alloc]initWithFrame:CGRectMake(self.isCreate.frame.origin.x, self.isCreate.frame.origin.y+20, self.alertview.frame.size.width-40, 100)];

self.attenL.font = [UIFont systemFontOfSize:15];

self.attenL.numberOfLines = 0;

self.attenL.font = [UIFont systemFontOfSize:14];

self.attenL.textColor = [UIColor blackColor];

[self.alertview addSubview:self.attenL];

[self.alertview addSubview:self.isCreate];

[self createBtnTitleWithCancel];

}

//創(chuàng)建button

- (void)createBtnTitleWithCancel

{

CGFloat m = self.alertview.frame.size.width;

//橫線和豎線

self.label1=[[UILabel alloc] initWithFrame:CGRectMake(0, self.alertview.height-50, self.alertview.width, 1)];

self.label1.backgroundColor=[UIColor lightGrayColor];

[self.alertview addSubview:self.label1];

self.label2=[[UILabel alloc] initWithFrame:CGRectMake(self.alertview.width/2, self.alertview.height-50, 1, 50)];

self.label2.backgroundColor=[UIColor lightGrayColor];

[self.alertview addSubview:self.label2];

//取消按鈕

self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

self.btn1.tag=1001;

self.btn1.frame = CGRectMake(0, self.alertview.height-50,m/2, 50);

[self.btn1 setTitleColor:TINTCOLOR forState:UIControlStateNormal];

[self.btn1 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

[self.btn1.titleLabel setFont:[UIFont systemFontOfSize:18]];

//確定按鈕

self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];

self.btn2.tag=1002;

self.btn2.frame = CGRectMake(m/2, self.alertview.height-50,m/2, 50);

[self.btn2 setTitleColor:TINTCOLOR forState:UIControlStateNormal];

[self.btn2 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

[self.btn2.titleLabel setFont:[UIFont systemFontOfSize:18]];

[self.alertview addSubview:self.btn1];

[self.alertview addSubview:self.btn2];

}

- (void)clickBtn:(UIButton *)btn

{

self.block(btn.tag-1000);

[self.blackView removeFromSuperview];

}

//傳值,并且高度自適應(yīng)

-(void)initWithTitle:(NSString *)title contentStr:(NSString *)content BtnArray:(NSArray *)array andBlock:(SBBlock)block

{

self.block=block;

CGFloat m = self.alertview.frame.size.width;

self.isCreate.text = title;

self.attenL.text= content;

if (array.count==1) {

[self.btn2 setTitle:[NSString stringWithFormat:@"%@", array[0]] forState:UIControlStateNormal];

}else{

[self.btn1 setTitle:[NSString stringWithFormat:@"%@", array[0]] forState:UIControlStateNormal];

[self.btn2 setTitle:[NSString stringWithFormat:@"%@", array[1]] forState:UIControlStateNormal];

}

CGRect r = [content boundingRectWithSize:CGSizeMake(self.alertview.width-40,1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.f]} context:nil];

self.attenL.frame = CGRectMake(self.isCreate.frame.origin.x, CGRectGetMaxY(self.isCreate.frame), self.alertview.frame.size.width-40, r.size.height);

if (array.count==1) {

self.btn2.frame=CGRectMake(0, CGRectGetMaxY(self.attenL.frame)+15, m, 50);

[self.btn1 removeFromSuperview];

[self.label2 removeFromSuperview];

}else{

self.btn1.frame=CGRectMake(0, CGRectGetMaxY(self.attenL.frame)+15, m/2, 50);

self.btn2.frame=CGRectMake(m/2, CGRectGetMaxY(self.attenL.frame)+15,m/2, 50);

}

self.label1.frame=CGRectMake(0, CGRectGetMaxY(self.attenL.frame)+15, self.alertview.width, 1);

self.label2.frame=CGRectMake(self.alertview.width/2,CGRectGetMaxY(self.attenL.frame)+15, 1, 50);

self.alertview.frame=CGRectMake(30, HEIGHT/2-120-64,WIDTH-60 ,CGRectGetMaxY(self.btn2.frame)+1);

}

--------------------------------分割線--------------------------------

使用:


SBAlertView *alertView = [[SBAlertView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

NSArray *array=@[@"退出",@"重新登錄"];//也可以只寫(xiě)一個(gè),看需要

[alertView initWithTitle:@"下線通知" contentStr:@"你的賬號(hào)在另一臺(tái)設(shè)備登錄。如非本人操作,則密碼可能已泄露,建議立即修改密碼或凍結(jié)賬號(hào)。" BtnArray:array andBlock:^(NSInteger num){

if (num == 1)

{

// 退出

NSLog(@"點(diǎn)擊退出");

}

else

{

NSLog(@"點(diǎn)擊重新登錄");

}

}];



寫(xiě)在后面:新手初試,不喜勿噴。感謝~(喜歡點(diǎn)??)

有不對(duì)的地方請(qǐng)告訴我,一起進(jìn)步。再次感謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,431評(píng)論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,637評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 178,555評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 63,900評(píng)論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,629評(píng)論 6 412
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 55,976評(píng)論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,976評(píng)論 3 448
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 43,139評(píng)論 0 290
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,686評(píng)論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,411評(píng)論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,641評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,129評(píng)論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,820評(píng)論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 35,233評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 36,567評(píng)論 1 295
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,362評(píng)論 3 400
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,604評(píng)論 2 380

推薦閱讀更多精彩內(nèi)容