纯代码实现基本控件

1.创建一个UIWindow

//初始化 [[UIScreen mainScreen] bounds 是一个Frame CGRect 也可以自定义 用CGRectMake
//self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];意思就是一个大小200*200 原点在0,0的框
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//定义背景颜色 可以用rgb颜色定义 r g b取值为0~1
//self.window.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
//或者使用图片
//UIImage *backImage = [UIImage imageNamed:@"background"];
//self.window.backgroundColor = [UIColor colorWithPatternImage:backImage];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

2.创建一个UIView

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
myView.backgroundColor = [UIColor redColor];
//把myView加载到UIWindow上 
[self.window addSubview:myView];
//设置myView的高度
[myView setFrame:CGRectMake(0,20,100,100)];

3.创建一个UIViewController

//MyFirstViewController 由自己定义在项目中
MyFirstViewController *viewController = [[MyFirstViewController alloc] initWithNibName:nil bundle:nil];
//viewController的view会被自动加载 具体看api帮助
self.window.rootViewController = viewController;

4.在UIViewController添加简单控件

在自定义的UIViewController添加UIButton UILabel 其余的类似 当然也可以加一个新的UIView

//添加UIButton
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 200, 50)];
button.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.6];
[button setTitle:@"test" forState:UIControlStateNormal];
[self.view addSubview:button];
//为button添加一个事件
[button addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside];
//添加UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 200, 50)];
label.text=@"test";
[self.view addSubview:label];

5.控制UIViewController跳转

模态(modal):a弹出b a为presenting view controller  b为presented view controller  

-(void)someButtonClicked
{
    [self presentViewController:self.aVC animated:YES completion:^{
        
    }];
}
消失 直接写在b中 已做优化
[self dismissViewControllerAnimated:YES completion:^{
        
    }];
如设置委托 在被弹出vc中设协议 弹出vc中实现 可实现传值 和任意层跳回 中间的层将直接消失 最后那个会有动画

弹出式的动画风格 设置presented VC的modalPresentationStyle属性

UIModalPresentationFullScreen;//默认的;
UIModalPresentationPageSheet;
UIModalPresentationFormSheet;
UIModalPresentationCurrentContext;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值