码迷,mamicode.com
首页 > 其他好文 > 详细

030在布局中实现一个模态对话框

时间:2015-06-15 01:43:06      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController
4 @end

ViewController.m

 1 #import "ViewController.h"
 2 #import "ModalDialog.h"
 3 
 4 @interface ViewController ()
 5 - (void)layoutUI;
 6 - (void)modalDidPush;
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     
14     [self layoutUI];
15 }
16 
17 - (void)didReceiveMemoryWarning {
18     [super didReceiveMemoryWarning];
19     // Dispose of any resources that can be recreated.
20 }
21 
22 - (void)layoutUI {
23     self.view.backgroundColor = [UIColor whiteColor];
24     UIButton *btnModal = [UIButton buttonWithType:UIButtonTypeRoundedRect];
25     btnModal.frame = CGRectMake(0, 0, 150, 40);
26     CGPoint newPoint = self.view.center;
27     newPoint.y += 10;
28     btnModal.center = newPoint;
29     btnModal.layer.masksToBounds = YES;
30     btnModal.layer.cornerRadius = 10.0;
31     btnModal.layer.borderColor = [UIColor brownColor].CGColor;
32     [btnModal setTitle:@"调用模态对话框" forState:UIControlStateNormal];
33     [btnModal addTarget:self action:@selector(modalDidPush) forControlEvents:UIControlEventTouchUpInside];
34     [self.view addSubview:btnModal];
35 }
36 
37 - (void)modalDidPush {
38     ModalDialog *dialog = [[ModalDialog alloc] init];
39     dialog.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
40     [self presentViewController:dialog animated:YES completion:nil];
41 }
42 
43 @end

ModalDialog.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ModalDialog : UIViewController
4 @end

ModalDialog.m

 1 #import "ModalDialog.h"
 2 
 3 @interface ModalDialog ()
 4 - (void)goodbyeDidPush;
 5 - (void)layoutUI;
 6 @end
 7 
 8 @implementation ModalDialog
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     
13     [self layoutUI];
14 }
15 
16 - (void)didReceiveMemoryWarning {
17     [super didReceiveMemoryWarning];
18     // Dispose of any resources that can be recreated.
19 }
20 
21 - (void)goodbyeDidPush {
22     //关闭模态对话框
23     [self dismissViewControllerAnimated:YES completion:nil];
24 }
25 
26 - (void)layoutUI {
27     UILabel *lblMessage = [[UILabel alloc] initWithFrame:self.view.bounds];
28     lblMessage.numberOfLines = 0; //标签默认显示是单行的,设置numberOfLines=0就表示自适应多行
29     lblMessage.font = [UIFont fontWithName:@"Helvetica-Bold" size:17]; //默认是17点素
30     lblMessage.text = @"您好,我是模态画面;想关闭我,请按Goodbye按钮";
31     lblMessage.textAlignment = NSTextAlignmentCenter;
32     lblMessage.textColor = [UIColor orangeColor];
33     lblMessage.backgroundColor = [UIColor darkGrayColor];
34     [self.view addSubview:lblMessage];
35     
36     UIButton *btnGoodbye = [UIButton buttonWithType:UIButtonTypeRoundedRect];
37     btnGoodbye.frame = CGRectMake(0, 0, 150, 40);
38     CGPoint newPoint = self.view.center;
39     newPoint.y += 80;
40     btnGoodbye.center = newPoint;
41     btnGoodbye.layer.masksToBounds = YES;
42     btnGoodbye.layer.cornerRadius = 10.0;
43     btnGoodbye.layer.borderColor = [UIColor whiteColor].CGColor;
44     btnGoodbye.layer.borderWidth = 2.0;
45     btnGoodbye.backgroundColor = [UIColor cyanColor];
46     [btnGoodbye setTitle:@"Goodbye" forState:UIControlStateNormal];
47     [btnGoodbye addTarget:self action:@selector(goodbyeDidPush) forControlEvents:UIControlEventTouchUpInside];
48     [self.view addSubview:btnGoodbye];
49 }
50 
51 @end

 

030在布局中实现一个模态对话框

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4576032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!