//1.程序入口 AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MainViewController * vc =[[MainViewController alloc]init];
UINavigationController * nav=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
//2.绘制,找到你自己创建的ViewController中的如下方法开始绘制吧
- (void)viewDidLoad {
[super viewDidLoad];
//设置全屏
[super.navigationController setNavigationBarHidden:true animated:TRUE];
[super.navigationController setToolbarHidden:true animated:TRUE];
//调用绘制标签函数
[self drawLabel];
//调用绘制单行文本框
[self drawTextField];
}
#pragma 创建标签
- (void)drawLabel{
//创建标签
UILabel *uiLabel=[[UILabel alloc]init];
//添加到当前view中,view属于当前ViewController
[self.view addSubview:uiLabel];
//设置标签位置大小
[uiLabel setFrame:CGRectMake(0, 0, 60, 40)];
//设置标签文本颜色
[uiLabel setTextColor:[UIColor whiteColor]];
//设置标签文本
[uiLabel setText:@"账号"];
}
#pragma 创建文本框
- (void)drawTextField{
//创建文本框、和UILabel一样
CUITextField *textField=[[CUITextField alloc]init];
[self.view addSubview:textField];
[textField setFrame:CGRectMake(0, 0, 540, 40)];
//调用textField一些基本方法....
[textField setTextColor:[UIColor redColor]];
//调用textField一些属性...
//是否为密码框
textField.isPwd=true;
//长度限制,这里不涉及中文长度处理
textField.maxLen=6;
}
#pragma 绘制button
-(void)drawButton{
UIButton *btn=[[UIButton alloc]init];
[self.view addSubview:btn];
[btn setFrame:CGRectMake(0, 0, 100, 40)];
//设置按钮上文字
[btn setTitle:@"I'm UIButton" forState:UIControlStateNormal];
//设置背景颜色
[btn setBackgroundColor:[UIColor redColor]];
//居中
btn.contentHorizontalAlignment=UICollectionViewScrollPositionCenteredHorizontally;
btn.titleLabel.textAlignment=NSTextAlignmentCenter;
// 设置button背景为圆效果
btn.layer.cornerRadius = 10;
//位置微调
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0,0,3,5)];
//文字大小
btn.titleLabel.font=[UIFont systemFontOfSize:20];
//设置title的字体居中
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
//设置title在一般情况下为白色字体
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//设置title在button被选中情况下为灰色字体
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
//添加点击事件
[btn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
}
//3.页面跳转
-(void)onClick{
NSLog(@"按钮点击事件");
//跳转到第二个页面TwoViewController
TwoViewController *vController = [[TwoViewController alloc] init];
[self.navigationController pushViewController:vController animated:true];
}
ios 初级入门 (一)
最新推荐文章于 2025-08-22 06:26:24 发布