您的位置:首页 > 产品设计 > UI/UE

UI控件

2019-03-17 23:00 31 查看

先学习几个UI控件吧

ios刚创建工程,已经默认创建了一个背景view,以后所有添加的东西都是在这个背景下,相当于这个view为所有控件的父类。

显示一个颜色背景

//显示背景
//创建一个uiview对象,设置左上角坐标,和宽高
UIView *myview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
myview.backgroundColor = [UIColor redColor];//设置背景颜色为红色
[self.view addSubview:myview];//将这个ui添加到当前的view 中

//每个子控件的位置,都是基于父控件的左上角
UIView *myview2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
myview2.backgroundColor = [UIColor blueColor];
[myview addSubview:myview2];//此背景的父类是上面的myview,因此坐标位置是基于父类的。

显示一段漂亮的文字

//显示文字
CGFloat viewh = self.view.bounds.size.width; //这一句可以直接获得相应view的宽等相关信息

UILabel *mylabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, viewh, 50)];
mylabel.text=@"我是一个uilable控件";
mylabel.textColor = [UIColor blueColor];//颜色
mylabel.font = [UIFont systemFontOfSize:20];//字号
mylabel.textAlignment = NSTextAlignmentCenter;//对齐方式居中-居左center改成left就行
[self.view addSubview:mylabel];

显示一张图片

图片先要添加到项目中,具体添加方式是:在文件名栏Assets.xcassets,将png照片拖拽进去

//显示图片
UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 200, 200)];
imgview.center = self.view.center;//设置居中
imgview.image = [UIImage imageNamed:@"sun"];//png图片,不需要写拓展名
[self.view addSubview:imgview];

一个文本框

//首先要定义一个private全局变量,tf
@implementation ViewController{
UITextField *tf;
}
//文本框输入
tf=[[UITextField alloc] initWithFrame:CGRectMake(20, 100, 300, 60)];
tf.placeholder=@"请输入密码";//文本框显示的提示
tf.backgroundColor=[UIColor cyanColor];//设置背景颜色
tf.secureTextEntry = YES;//输入的具体内容不可见,用·代替了
[self.view addSubview:tf];

//tf.text获取文本框的内容

添加一个有响应事件的按钮+获取输入值

UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(20, 200, 300, 60)];
[btn setTitle:@"确认" forState:UIControlStateNormal]//按钮的样式
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//按钮上文字的颜色
[btn setBackgroundColor:[UIColor blueColor]];//按钮的背景色
[btn addTarget:self action:@selector(btnClick)
forControlEvents:UIControlEventTouchUpInside];//此处的btnClick是响应回调函数的函数名
[self.view addSubview:btn];
//定义一个点击相应回调函数,写入回调事件
-(void)btnClick{
NSLog(@"你输入的是:%@",tf.text);//获取文本框内的内容
}

对话框提示

//主函数调用对话框
[self viewDidAppear:true];
//新建一个函数
-(void) viewDidAppear:(BOOL)animated{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"提示" preferredStyle:UIAlertControllerStyleAlert];//设置样式和内容

UIAlertAction *ac1=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){//确定点击事件
NSLog(@"点击确定了");
} ];
UIAlertAction *ac2=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击取消了");
}];
[alert addAction:ac1];//将两个点击事件加载到对话框上
[alert addAction:ac2];

[self presentViewController:alert animated:false completion:nil];//显示对话框
}

进度条

//进度条
UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(90, 260, 200, 2)];
progressView.transform = CGAffineTransformMakeScale(1, 8);
progressView.progress = 0.
4000
5;//设置进度条的进度
progressView.center =  self.view.center;
[self.view addSubview:progressView];

小菊花控件

//小菊花控件
UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
loading.color = [UIColor blackColor];
loading.center = self.view.center;
[loading startAnimating];//开始旋转
[self.view addSubview:loading];

loading.hidden = NO;//是否隐藏小菊花,可以吧把oading作为全局变量,控制小菊花是否隐藏

浏览器控件
先要开始浏览器的功能:info.plist,添加app transport security settings目录类型,再在此dict下添加bool类型的allow arbitrary loads 值为yes。

UIWebView  *myweb = [[UIWebView alloc] init];//创建一个URL的view
myweb.frame = self.view.frame;//全屏
NSURL *url=[NSURL URLWithString:@"https://www.163.com"];//创建一个URL对象
NSURLRequest *request=[NSURLRequest requestWithURL:url];//创建URL请求对象
[myweb loadRequest:request];//用view加载请求对象包含的URL
[self.view addSubview:myweb];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: