IOS入门-基础控件

这篇博客详细介绍了iOS开发中的基础UI控件,包括UILabel、UIButton、UIView、UITextView、UIImageView等,以及复杂的UITableView和UICollectionView的创建与使用。此外,还涉及到了UIWebView、UIAlertView和UINavigationBar等组件的应用。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值