基础控件
-
- UILable(文本标签)
- UIButton(按钮)
- UIView(视图)
- UITextView(文本输入框)
- UIImageView(图片显示)
- UITextView(能滚动的文字显示控件)
- UIActivityIndicator(菊花圈圈)
- UISwitch(开关按钮)
- UIPickView(滚轮选择器)
- UIDatePicker(日期选择器)
- UIToolBar(工具条)
- UIProgressView(进度条)
- UISlider(滑块)
- UISegmentControl(选项卡)
- UIPageControl(分页控件)
- UIScrollView(滑动的控件)
- UITableView(表格)
- UICollectionView(九宫格)
- UIWebView (网页显示控件)
- UIAlertView (对话框(中间弹框))
- UINavigationBar(导航条)
UILable(文本标签)
// ViewController.m
// UILabel
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//UILabel
UILabel *label = [[UILabel alloc]init];
label.text = @"文本控件";
label.font = [UIFont systemFontOfSize:20];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor redColor];
label.numberOfLines = 0;//0 不限制行数
label.textAlignment = NSTextAlignmentCenter;//right center
[self.view addSubview:label];
label.frame = CGRectMake(20, 20, 100, 100);
}
@end
UIButton(按钮)
// ViewController.m
// UIButton
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//UIControlStateHighlighted:按钮按下时状态
UIButton *button=[[]UIButton buttomWithType:UIButtonTypeCustom]
[button setTitle:@"按钮" forState:UIControlStateNormal];
[button setTitle:@"按下高亮" forState:UIControlStateHighlighted];
UIColor *color = [UIColor redColor];
[button setBackgroundColor:color];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
button.frame = CGRectMake(0, 0, 100, 100);
}
- (void)buttonClick:(UIButton *)sender{
NSLog(@"按钮点击触发");
}
@end
UIView(视图)
UIView是窗口上的一块区域,是iOS中所有控件的基类
// ViewController.m
// UIView
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
UIView *customView = [[UIView alloc]init];
customView.backgroundColor = [UIColor redColor];
[self.view addSubview:customView];
customView.frame = CGRectMake(0, 0, 100, 100);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"按钮" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor whiteColor]];
[customView addSubview:button];
button.frame = CGRectMake(0, 0,customView.frame.size.width, 30);
}
@end
UITextView(文本输入框)
// ViewController.m
// UITextField
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//UItextField
UITextField *textField = [[UITextField alloc]init];
textField.delegate = self;
textField.backgroundColor = [UIColor redColor];
textField.textColor = [UIColor whiteColor];
textField.font = [UIFont systemFontOfSize:18];
textField.placeholder = @"请输入账户名";
[self.view addSubview:textField];
textField.frame = CGRectMake(0, 50, self.view.frame.size.width, 50);
}
//指明是否允许在按下回车键时结束编辑
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"开始编辑");
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"编辑结束 = %@",textField.text);
}
@end
UIImageView(图片显示)
// ViewController.m
// UIImageView
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"image_pic"];
UIImageView *imageView = [[UIImageView alloc]init];
imageView.image = image;
imageView.backgroundColor = [UIColor blackColor];
//UIViewContentModeScaleToFill,表示通过缩放来填满view,也就是说图片会变形。
//UIViewContentModeScaleAspectFit,表示按比例缩放并且图片要完全显示出来,意味着view可能会留有空白
//UIViewContentModeScaleAspectFill,表示按比例缩放并且填满view,意味着图片可能超出view,可能被裁减掉
imageView.contentMode = UIViewContentModeScaleAspectFill;//,UIViewContentModeScaleAspectFit,UIViewContentModeScaleAspectFill
imageView.clipsToBounds = YES;
[self.view addSubview:imageView];
imageView.frame = CGRectMake(0, 0, 100, 100);
}
@end
UITextView(能滚动的文字显示控件)
// ViewController.m
// UITextView
#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@property(nonatomic,strong)UITextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.textView = [[UITextView alloc]init];
self.textView.delegate = self;
self.textView.textColor = [UIColor blackColor];
self.textView.layer.borderWidth = 0.5;
self.textView.layer.borderColor = [UIColor redColor].CGColor;
[self.view addSubview:self.textView];
self.textView.frame = CGRectMake(20, 200, 300, 200);
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(@"开始编辑");
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
NSLog(@"结束编辑 = %@",textView.text);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self