IOS入门-基础控件

基础控件

    • UILable(文本标签)
    • UIButton(按钮)
    • UIView(视图)
    • UITextView(文本输入框)
    • UIImageView(图片显示)
    • UITextView(能滚动的文字显示控件)
    • UIActivityIndicator(菊花圈圈)
    • UISwitch(开关按钮)
    • UIPickView(滚轮选择器)
    • UIDatePicker(日期选择器)
    • UIToolBar(工具条)
    • UIProgressView(进度条)
    • UISlider(滑块)
    • UISegmentControl(选项卡)
    • UIPageControl(分页控件)
    • UIScrollView(滑动的控件)
    • UITableView(表格)
      • 创建一个UITableViewCell
      • 创建另一个UITableViewCell
      • 使用UITableViewCell创建UITableView
    • UICollectionView(九宫格)
      • 创建一个UITableViewCell
      • 使用UITableViewCell创建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.textView resignFirstResponder];
}


@end

UIActivityIndicator(菊花圈圈)

//
//  ViewController.m
//  UIActivityIndicatorView

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UIActivityIndicatorView *activityIndicatorView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.activityIndicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicatorView.color = [UIColor whiteColor];
    self.activityIndicatorView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.activityIndicatorView];
    self.activityIndicatorView.frame = CGRectMake(100, 100, 100, 100);
    self.activityIndicatorView.hidesWhenStopped = NO;
    self.activityIndicatorView startAnimating
}

@end

UISwitch(开关按钮)

//
//  ViewController.m
//  UISwitch

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UISwitch *mySwitch = [[UISwitch alloc]init];
    //内置控件的大小
    mySwitch.on = YES;
    mySwitch.tintColor = [UIColor redColor];//关闭状态的边框颜色
    mySwitch.onTintColor = [UIColor yellowColor];
    mySwitch.thumbTintColor = [UIColor blueColor];
    [self.view addSubview:mySwitch];
    mySwitch.frame = CGRectMake(100, 100, 300, 300);
    
    [mySwitch addTarget:self action:@selector(click) forControlEvents:UIControlEventValueChanged];
    
}

- (void)click{
    NSLog(@"触发");
}

@end


UIPickView(滚轮选择器)

//  ViewController.m
//  UIPickerView
#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic,strong)UIPickerView *pickerView;

@property(nonatomic,strong)NSMutableArray *numberArray;

@property(nonatomic,strong)NSMutableArray *titleArray;

@property(nonatomic,copy)NSString *selctedNumStr;

@property(nonatomic,copy)NSString *selectedTitleStr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //UIPickerView
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    self.pickerView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.pickerView];
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    
    
    self.numberArray = [[NSMutableArray alloc]init];
    for (int i = 0; i<10; i++) {
        
        NSString *tempStr = [NSString stringWithFormat:@"%d",i];
        
        [self.numberArray addObject:tempStr];
    }
    
    self.titleArray = [[NSMutableArray alloc]init];
    for (int i = 0; i<5; i++) {
        NSString *tempStr = [NSString stringWithFormat:@"title%d",i];
        [self.titleArray addObject:tempStr];
    }

}
//有几列

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

///设置指定列包含的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.numberArray.count;
    }else{
        return self.titleArray.count;
    }
}
//每个选项显示的内容
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return self.numberArray[row];
    }else{
        return self.titleArray[row];
    }
}
//滚轮高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}
//滚轮宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (component == 0) {
        return 50;
    }
    return 200;
}
//获取当前选中的选项
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component == 0) {
        NSString *numStr = self.numberArray[row];
        NSLog(@"numStr = %@",numStr);
        self.selctedNumStr = numStr;
    }else{
        NSString *titleStr = self.titleArray[row];
        self.selectedTitleStr = titleStr;
    }
    
}

@end


UIDatePicker(日期选择器)

//  ViewController.m
//  UIDatePicker

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UIDatePicker *datePicker;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    UIDatePicker
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    [self.view addSubview:self.datePicker];
    [self.datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    //默认显示今天
    //最小时间 离当前时间:明天
    self.datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:86400];
    //最大时间 离当前时间:后天
    self.datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:172800];
    [self.datePicker setDatePickerMode:UIDatePickerModeTime];
    
}

- (void)dateChange:(UIDatePicker *)sender{
    
    NSLog(@"sender.date = %@",sender.date);
}


@end

UIToolBar(工具条)

//  ViewController.m
//  UIToolBar
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //UIToolBar
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    toolBar.barStyle = UIBarStyleDefault;
    toolBar.backgroundColor = [UIColor orangeColor];
    
    //UIBarButtonItem
    [self.view addSubview:toolBar];
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAction:)];
    
    
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:NULL];
    
    
    UIBarButtonItem *addItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)];
    
    [toolBar setItems:@[cancelItem,spaceItem,addItem] animated:YES];
    
}

- (void)cancelAction:(UIBarButtonItem *)sender{
    NSLog(@"取消");
}

- (void)addAction{
    NSLog(@"添加");
}

@end

UIProgressView(进度条)

//  ViewController.m
//  UIProgressView
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // UIProgressView
    UIProgressView *progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    progressView.progress = 0.5;//表示进度
    progressView.progressTintColor = [UIColor redColor];
    progressView.trackTintColor = [UIColor greenColor];
    progressView.progressViewStyle = UIProgressViewStyleBar;
    [self.view addSubview:progressView];    
}
@end

UISlider(滑块)

//  ViewController.m
//  UISlider

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)UISlider *slider;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // UISlider
    self.slider = [[UISlider alloc]init];
    self.slider.minimumTrackTintColor = [UIColor greenColor];
    self.slider.maximumTrackTintColor = [UIColor redColor];
    [self.slider setThumbImage:[UIImage imageNamed:@"椭圆5"] forState:UIControlStateNormal];
    
    [self.slider addTarget:self action:@selector(changeProgress:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:self.slider];
    self.slider.frame = CGRectMake(100, 100, 200, 30);
   
}

- (void)changeProgress:(UISlider *)sender{
    NSLog(@"进度 = %f",sender.value);
}
@end


UISegmentControl(选项卡)

//  ViewController.m
//  UISegmentControl

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //UISegmentControl
    
    NSArray *data = @[@"1",@"2",@"3"];
    
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:data];
    [segment addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
    [segment insertSegmentWithTitle:@"4" atIndex:3 animated:YES];
    [segment removeSegmentAtIndex:0 animated:YES];
    [segment setTintColor:[UIColor greenColor]];
    [segment setBackgroundColor:[UIColor redColor]];
    
    [self.view addSubview:segment];
    segment.frame = CGRectMake(20, 20, 200, 44);
}

- (void)click:(UISegmentedControl *)sender{
    NSLog(@"第%ld个",(long)sender.selectedSegmentIndex);
}
@end

UIPageControl(分页控件)

**一般UIPageControl会配合ScrollView一起使用

//  ViewController.m
//  UIPageControl
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    pageControl.numberOfPages = 5;
    pageControl.currentPage = 1;
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    [self.view addSubview:pageControl];
    pageControl.frame = CGRectMake(0, 100, self.view.frame.size.width, 50);
    pageControl.backgroundColor = [UIColor redColor];
}


@end

UIScrollView(滑动的控件)

**一般UIPageControl会配合ScrollView一起使用

//  ViewController.m
//  UIScrollView

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //UIScrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-100)];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height);
    self.scrollView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.scrollView];
    
    UIView *view1 = [[UIView alloc]init];
    view1.backgroundColor = [UIColor redColor];
    [self.scrollView addSubview:view1];
    view1.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height-100);
    
    
    UIView *view2 = [[UIView alloc]init];
    view2.backgroundColor = [UIColor yellowColor];
    [self.scrollView addSubview:view2];
    view2.frame = CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height - 100);
    
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
   
    self.scrollView.scrollEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = YES;
    self.scrollView.showsHorizontalScrollIndicator = YES;
    
    self.scrollView.delegate = self;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"滚动的时候触发");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"结束滑动");
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSLog(@"结束滚动");
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width, 0) animated:YES];
}


@end

UITableView(表格)

创建一个UITableViewCell

//  TestCell.h
//  UITableView

#import 

NS_ASSUME_NONNULL_BEGIN

@interface TestCell : UITableViewCell

@end

NS_ASSUME_NONNULL_END

//  TestCell.m
//  UITableView
#import "TestCell.h"

@implementation TestCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

@end

创建另一个UITableViewCell

//  Test2Cell.h
//  UITableView
#import 

NS_ASSUME_NONNULL_BEGIN

@interface Test2Cell : UITableViewCell

@end

NS_ASSUME_NONNULL_END

//  Test2Cell.m
//  UITableView
#import "Test2Cell.h"

@implementation Test2Cell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        self.contentView.backgroundColor = [UIColor yellowColor];
        self.backgroundColor = [UIColor yellowColor];
    }
    return self;
}

@end

使用UITableViewCell创建UITableView

//  ViewController.m
//  UITableView

#import "ViewController.h"
#import "TestCell.h"
#import "Test2Cell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //UITableView  重点
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    //uitableviewCell
    [self.tableView registerClass:[TestCell class] forCellReuseIdentifier:@"TestCell"];
    [self.tableView registerClass:[Test2Cell class] forCellReuseIdentifier:@"Test2Cell"];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 10;
    }
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell" forIndexPath:indexPath];
        
        cell.textLabel.text = [NSString stringWithFormat:@"第%ld组 第%ld行",(long)indexPath.section,indexPath.row + 1];
        
        return cell;
        
    }
    
    Test2Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Test2Cell" forIndexPath:indexPath];
    
   cell.textLabel.text = [NSString stringWithFormat:@"第%ld组 第%ld行",(long)indexPath.section,indexPath.row + 1];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 50;
    }
    return 78;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGFLOAT_MIN;
    }
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return  nil;
    }
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor redColor];
    return view;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"我触发了 组 = %ld  行 = %ld",(long)indexPath.section,(long)indexPath.row);
}

@end

UICollectionView(九宫格)

创建一个UITableViewCell

//  TestCell.h
//  UITableView

#import 

NS_ASSUME_NONNULL_BEGIN

@interface TestCell : UITableViewCell

@end

NS_ASSUME_NONNULL_END

//  TestCell.m
//  UITableView
#import "TestCell.h"

@implementation TestCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

@end

使用UITableViewCell创建UICollectionView

//
//  ViewController.m
//  UICollectionView
#import "ViewController.h"
#import "TestCell.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //UICollectionView
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    layout.minimumLineSpacing = 20;
    layout.minimumInteritemSpacing = 20;
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 20*3 - 1) / 4.0, (self.view.frame.size.width - 20*2) / 4.0 + 15);
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor redColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    //必须复用
    [self.collectionViewregisterClass:[TestCell class] forCellReuseIdentifier:@"TestCell"];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 10;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TestCell" forIndexPath:indexPath];
    
    cell.contentLabel.text = [NSString stringWithFormat:@"第%ld个",(long)indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"第%ld个",indexPath.row + 1);
    //把indexpath.row
    if (self.didSelected) {
        self.didSelected(indexPath.row);
    }
}



@end

UIWebView (网页显示控件)

//
//  ViewController.m
//  UIWebView

#import "ViewController.h"
#import 

@interface ViewController ()<WKNavigationDelegate>

@property(nonatomic,strong)WKWebView *webview;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //WKWebView
    
    self.webview = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.webview.backgroundColor = [UIColor redColor];
    self.webview.scrollView.backgroundColor = [UIColor yellowColor];
    self.webview.navigationDelegate = self;
    [self.view addSubview:self.webview];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com/"]];
    
    [self.webview loadRequest:request];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    NSLog(@"加载完成");
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    NSLog(@"加载失败");
}


@end

UIAlertView (对话框(中间弹框))

//
//  ViewController.m
//  UIAlertController
//
//  Created by clz on 2019/8/25.
//  Copyright © 2019年 clz. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //UIAlertController
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确认删除吗?" preferredStyle:UIAlertControllerStyleAlert];
//
//    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        NSLog(@"我点击了确定");
//    }];
//
//    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        NSLog(@"我选择了取消");
//    }];
//
//    [alert addAction:action];
//    [alert addAction:cancel];
//
//    [self presentViewController:alert animated:YES completion:^{
//
//    }];
    
    
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"拍照" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"我点击了确定");
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"我点击了确定");
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"我选择了取消");
    }];
    
    [alert addAction:action];
    [alert addAction:action1];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    
}


@end

UINavigationBar(导航条)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor redColor];
    
    self.navigationItem.title = @"导航栏";
   
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:nil];

    self.navigationItem.backBarButtonItem = backItem;
    
}


你可能感兴趣的:(IOS入门-基础控件)