iOS基础之控件(篇2)

目录

    1. UIPickerView  数据选择器
    2. UIDatePicker  时间选择器
    3. UISwitch      开关
    4. UISearchBar   搜索
    5. UIStepper     步进器
    6. UIActivityIndicatorView   网络菊花
    7. UISegmentedControl        分段选择器
    8. UISlider                  滑块
    9. UIProgressView            进度条
    10. UIAlertController        弹框
1. UIPickerView
    // 创建pickerView
    UIPickerView *contentPickV=[UIPickerView new];
    [self.view addSubview:contentPickV];
    [contentPickV autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [contentPickV autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [contentPickV autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
    [contentPickV autoSetDimension:ALDimensionHeight toSize:140];


    // 获取 某组的选中行数
    NSInteger selectedRow=[contentPickV selectedRowInComponent:0];
    // 获取 组数
    NSInteger numOfComs=[contentPickV numberOfComponents];
    // 获取 某组的行数
    NSInteger numOfRows=[contentPickV numberOfRowsInComponent:0];
    // 获取 某组的行大小
    CGSize rowSize=[contentPickV rowSizeForComponent:0];
    // 获取 某组的行View
    UIView *rowView=[contentPickV viewForRow:0 forComponent:0];
    
    // 选中 某行
    [contentPickV selectRow:0 inComponent:0 animated:true];
    // 刷新 某组
    [contentPickV reloadComponent:0];
    // 刷新 所有组
    [contentPickV reloadAllComponents];
    // dele
    [contentPickV setDelegate:self];
    [contentPickV setDataSource:self];

#pragma mark UIPickerViewDelegate,UIPickerViewDataSource
// 1.组数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
// 2.行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataSourceArr.count;
}
// 3.行宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 100;
}
// 4.行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 50;
}
// 5.行标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.dataSourceArr[row];
}
// 5.行复杂标题
-(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.dataSourceArr[row];
}
// 5.行View
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    return [UIView new];
}
// 6.选择行后调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
}
2. UIDatePicker 时间选择器
    // 创建时间选择器(UIDatePicker: UIControl)
    //(一般使用继承UIPickView的自定义日期选择器)
    UIDatePicker *datePickView=[UIDatePicker new];
    [self.view addSubview:datePickView];
    [datePickView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];


    // 设置Mode
    [datePickView setDatePickerMode:UIDatePickerModeTime];
    /*
     UIDatePickerModeTime   上午 1 20
     UIDatePickerModeDate   2022 1 1
     UIDatePickerModeDateAndTime 2022 1 1 上午 1 20(默认)
     UIDatePickerModeCountDownTimer 1 20
     */
    // 设置 初始日期 / 日期
    [datePickView setDate:[NSDate date]];
    [datePickView setDate:[NSDate date] animated:true];
    // 设置 最大最小可选date(超过范围的日期依旧会显示,只是滑动到那后立刻滚动到范围内)
    [datePickView setMaximumDate:[NSDate date]];
    [datePickView setMinimumDate:[NSDate date]];
    // 设置min间隔值(1~30)
    [datePickView setMinuteInterval:5];
    // 获取 当前选中的date
    NSDate *date=datePickView.date;
    // changedTarget
    [datePickView addTarget:self action:@selector(handleDatePickView:) forControlEvents:UIControlEventValueChanged];

    // 仅在cutdown模式下有效,
    [datePickView setCountDownDuration:5.0];
    // 设置locale(默认:如下,置nil为默认)
    [datePickView setLocale:[NSLocale currentLocale]];
    [datePickView setCalendar:[NSCalendar currentCalendar]];
    [datePickView setTimeZone:nil];
3.UISwitch 开关
    // 创建开关Switch(UISwitch : UIControl)
    // 大小固定---可通过改变形变来改大小
    UISwitch *openSwitch=[UISwitch new];
    [self.view addSubview:openSwitch];
    [openSwitch autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];

    // 获取 是否开启
    BOOL isOn=openSwitch.isOn;
    // 设置 是否开启
    [openSwitch setOn:true];
    [openSwitch setOn:true animated:true];
    // 设置 左边图片(开启可见)
    [openSwitch setOnImage:[UIImage new]];
    // 设置 右边图片(关闭可见)
    [openSwitch setOffImage:[UIImage new]];
    // 设置 左边填充色(开启可见)
    [openSwitch setOnTintColor:[UIColor blueColor]];
    // 设置 右边填充色(关闭可见)
    [openSwitch setTintColor:[UIColor redColor]];
    // 设置 中间圆填充色
    [openSwitch setThumbTintColor:[UIColor whiteColor]];

    // 添加值改变事件(changeTargt状态改变后调用)
    [openSwitch addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventValueChanged];
4. UISearchBar 搜索
    // 创建(SearchBar: UIView)
    // 通常自定义UITexField
    UISearchBar *searchBar=[UISearchBar new];
    [self.view addSubview:searchBar];
    [searchBar autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];


    // 设置 style
    [searchBar setBarStyle:UIBarStyleDefault];
    /*
     UIBarStyleDefault          = 0,
     UIBarStyleBlack            = 1,
     UIBarStyleBlackOpaque      = 1, // 同Black(不建议使用)
     UIBarStyleBlackTranslucent = 2, // Black+translucent为true(不建议使用)
     */
    // 设置 text
    [searchBar setText:@""];
    // 设置 placeHolder
    [searchBar setPlaceholder:@""];
    // 设置 prompt?
    [searchBar setPrompt:@""];
    
    // 设置 是否显示cancelButton(默认:false)
    [searchBar setShowsCancelButton:true];
    [searchBar setShowsCancelButton:true animated:true];
    // 设置 是否显示内右侧book图标(默认:false)
    [searchBar setShowsBookmarkButton:true];
    // 设置 是否显示展开建议视图(默认:false)
    [searchBar setShowsSearchResultsButton:true];
    // 设置 展开按钮是否选中
    [searchBar setSearchResultsButtonSelected:true];
    // 获取 展开按钮是否选中
    BOOL searchResultsButtonSelected=searchBar.isSearchResultsButtonSelected;

    
    // 设置 填充色,背景色
    [searchBar setTintColor:[UIColor redColor]];
    [searchBar setBarTintColor:[UIColor blueColor]];
    // 设置 是否半透明(默认:false)
    [searchBar setTranslucent:true];
    // 获取 是否半透明
    BOOL translucent=searchBar.isTranslucent;
    // 设置 背景图片
    [searchBar setBackgroundImage:[UIImage imageNamed:@""]];
    [searchBar setBackgroundImage:[UIImage imageNamed:@""] forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];
    UIImage *img=[searchBar backgroundImageForBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];
    // 设置 tf背景图片
    [searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    UIImage *seachBg=[searchBar searchFieldBackgroundImageForState:UIControlStateNormal];
    // 设置 右侧图标(UISearchBarIconClear:clear图标 UISearchBarIconSearch:搜索图标 UISearchBarIconBookmark:书签图标 UISearchBarIconResultsList显示建议图标)
    [searchBar setImage:[UIImage imageNamed:@""] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
    UIImage *searchIconBg=[searchBar imageForSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
    
    // 设置 键盘上方View
    [searchBar setInputAccessoryView:[UIView new]];
    // 设置 搜索文本位置
    [searchBar setSearchTextPositionAdjustment:UIOffsetMake(10, 5)];
    UIOffset textOffset=[searchBar searchTextPositionAdjustment];
    // 设置 图标位置
    [searchBar setPositionAdjustment:UIOffsetMake(10, 5) forSearchBarIcon:UISearchBarIconClear];
    UIOffset Offset=[searchBar positionAdjustmentForSearchBarIcon:UISearchBarIconClear];
    // 设置 背景图片位置
    [searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetZero];
    UIOffset bgOffset=[searchBar searchFieldBackgroundPositionAdjustment];
    
    // ?
    [searchBar setScopeButtonTitles:@[@"",@"",@""]];
    //
    [searchBar setSelectedScopeButtonIndex:0];
    NSInteger index=searchBar.selectedScopeButtonIndex;
    // ?,默认:nil
    [searchBar setShowsScopeBar:true];
    //
    [searchBar setScopeBarBackgroundImage:[UIImage new]];
    UIImage *scopeBgImg=[searchBar scopeBarBackgroundImage];
    //
    [searchBar setScopeBarButtonBackgroundImage:[UIImage new] forState:UIControlStateNormal];
    UIImage *scopeBGImg=[searchBar scopeBarButtonBackgroundImageForState:UIControlStateNormal];
    //
    [searchBar setScopeBarButtonDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];
    UIImage *divedeImg=[searchBar scopeBarButtonDividerImageForLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];
    //
    [searchBar setScopeBarButtonTitleTextAttributes:@{} forState:UIControlStateNormal];
    NSDictionary *attributeDic=[searchBar scopeBarButtonTitleTextAttributesForState:UIControlStateNormal];

dele

    // 
    [searchBar setDelegate:self];  

// 文本内容改变前调用(是否允许改变)
-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{return true;}
// 文本内容改变后调用
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{}

// 是否允许编辑
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{}
// 开始编辑后调用
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{}
// 是否允许结束编辑
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{}
// 结束编辑后调用
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{}

// 点击cancel后调用
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{}
// 点击搜索按钮后调用
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{}
// 点击书签按钮后调用
-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar{}
// 点击展开建议View后调用
-(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar{}
// 
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{}
5. UIStepper 步进器
    UIStepper *stepper=[UIStepper new];
    [self.view addSubview:stepper];
    
    // 设置 背景色
    [stepper setBackgroundColor:[UIColor blueColor]];
    // 设置 背景图片
    [stepper setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    // 设置 加减的填充色
    [stepper setTintColor:[UIColor blueColor]];
    // 设置 加号图片
    [stepper setIncrementImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    // 设置 减号图片
    [stepper setDecrementImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    // 设置 最小值
    [stepper setMinimumValue:0];
    // 设置 最大值
    [stepper setMaximumValue:10];
    // 设置 当前值
    [stepper setValue:0.5];
    // 设置 间隔值(最后不够时直接跳到最大值)(默认1,必须大于0)
    [stepper setStepValue:0.1];
    // 设置 min <-> max(默认false)最大值后再加跳到最小值,最小值同理
    [stepper setWraps:true];
    // 设置 按下时是否重复触发事件(默认true)
    [stepper setAutorepeat:true];
    
    // 添加 值改变事件
    [stepper addTarget:self action:@selector(handleStepper:) forControlEvents:UIControlEventValueChanged];
6. UIActivityIndicatorView 网络菊花
    //
    UIActivityIndicatorView *acV=[UIActivityIndicatorView new];
    UIActivityIndicatorView *acV2=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    /*
     UIActivityIndicatorViewStyleWhiteLarge,
     UIActivityIndicatorViewStyleWhite,         默认
     UIActivityIndicatorViewStyleGray
     */
    [self.view addSubview:acV];
    
    // 设置 颜色
    [acV setColor:[UIColor blueColor]];
    // 设置 停止转的时候是否隐藏(默认true)
    [acV setHidesWhenStopped:true];
    // 开始转
    [acV startAnimating];
    // 停止转
    [acV stopAnimating];
    // 是否在转
    BOOL isAni=[acV isAnimating];
    
    // 设置 背景色
    [acV setBackgroundColor:[UIColor blueColor]];
7. UISegmentedControl 分段选择器

    //
    UISegmentedControl *segC=[[UISegmentedControl alloc]initWithItems:@[@"section1",@"section2"]];
    [self.view addSubview:segC];
    
    // 设置 背景色
    [segC setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    // 设置 是否保留选中状态
    [segC setMomentary:true];
    // 设置 是否按需分配宽度(默认false:统一长度)
    [segC setApportionsSegmentWidthsByContent:true];
    // 选中 指定项
    [segC setSelectedSegmentIndex:0];
    // 设置 指定section是否可用
    [segC setEnabled:true forSegmentAtIndex:0];
    // 设置 填充色(选中项的背景色)
    [segC setTintColor:[UIColor blueColor]];
    // 设置 指定section标题
    [segC setTitle:@"section001" forSegmentAtIndex:0];
    [segC setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} forState:UIControlStateNormal];
    // 设置 指定section图片
    [segC setImage:[UIImage imageNamed:@""] forSegmentAtIndex:0];
    // 设置 指定section内容偏移
    [segC setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:0];
    // 设置 指定section宽度
    [segC setWidth:100 forSegmentAtIndex:0];

    // 添加 图片选项
    [segC insertSegmentWithImage:[UIImage imageNamed:@""] atIndex:0 animated:true];
    // 添加 标题选项
    [segC insertSegmentWithTitle:@"" atIndex:0 animated:true];
    // 移除 指定选项
    [segC removeSegmentAtIndex:0 animated:true];
    // 移除 所有选项
    [segC removeAllSegments];
    
    // 设置 分割线图片
    [segC setDividerImage:[UIImage imageNamed:@""] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    // 获取 项部分内容偏移量
    [segC setContentPositionAdjustment:UIOffsetMake(10, 10) forSegmentType:UISegmentedControlSegmentLeft barMetrics:UIBarMetricsDefault];
    /*
     UISegmentedControlSegmentAny       影响所有标签
     UISegmentedControlSegmentLeft      影响只有左边部分
     UISegmentedControlSegmentCenter    影响只有中间部分
     UISegmentedControlSegmentRight     影响只有右边部分
     UISegmentedControlSegmentAlone     在仅有一个标签时生效
     */

    // 添加 值改变事件
    [segC addTarget:self action:@selector(handleSegC:) forControlEvents:UIControlEventValueChanged];
8. UISlider 滑块
    UISlider *slider=[UISlider new];
    [self.view addSubview:slider];
    
    // 设置 最小值、最大值、当前值、是否滑动停止后才触发ValueChanged事件(默认true)
    [slider setMinimumValue:0];
    [slider setMaximumValue:10];
    [slider setValue:5];
    [slider setContinuous:true];
    // 设置 圆图片、左侧图片、右侧图片
    [slider setThumbImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [slider setMaximumTrackImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [slider setMinimumTrackImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    // 设置 圆填充色、左侧填充色、右侧填充色
    [slider setThumbTintColor:[UIColor blueColor]];
    [slider setMinimumTrackTintColor:[UIColor blueColor]];
    [slider setMaximumTrackTintColor:[UIColor redColor]];
    // 设置 最左边图片(默认nil)、最右边图片(默认nil)
    [slider setMinimumValueImage:[UIImage imageNamed:@""]];
    [slider setMaximumValueImage:[UIImage imageNamed:@""]];
    // 添加 值改变事件
    [slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];
9. UIProgressView 进度条
    //
    UIProgressView *progressView=[UIProgressView new];
    [progressView setProgressViewStyle:UIProgressViewStyleBar];
    /*
     UIProgressViewStyleDefault
     UIProgressViewStyleBar
     */
    [self.view addSubview:progressView];
    
    // 设置 当前进度(0~1(当前值-最小值)/两值之差),不能修改最大最小值
    [progressView setProgress:0.5];
    [progressView setProgress:0.5 animated:true];
    // 设置 填充色、已过进度条颜色(优先级高于tintColor,覆盖)、剩余进度条颜色
    [progressView setTintColor:[UIColor blueColor]];
    [progressView setProgressTintColor:[UIColor blueColor]];
    [progressView setTrackTintColor:[UIColor redColor]];
    // 设置 已过进度条图片、剩余进度条图片
    [progressView setProgressImage:[UIImage imageNamed:@""]];
    [progressView setTrackImage:[UIImage imageNamed:@""]];
10. UIAlertController 弹框
    // 创建 alert(不能放在viewDidLoad中,可以放在viewDidAppear中)(必须在主线程中弹出)
    UIAlertController *alertC=[UIAlertController alertControllerWithTitle:@"标题" message:@"内容" preferredStyle:UIAlertControllerStyleAlert];
    /*
     UIAlertControllerStyleActionSheet
     UIAlertControllerStyleAlert
     */
    // 添加 输入框(可选)
    [alertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        //
        [textField setPlaceholder:@""];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleTFChange:) name:UITextFieldTextDidChangeNotification object:nil];
    }];
    // 添加 子项
    [alertC addAction:[UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 获取输入框
        UITextField *firstTF=[[alertC textFields]firstObject];
    }]];
    /*
     UIAlertActionStyleDefault      默认
     UIAlertActionStyleCancel       取消(用于取消)
     UIAlertActionStyleDestructive  销毁(用于删除)
     */
    // 弹出 alert
    [self presentViewController:alertC animated:true completion:^{
    }];

你可能感兴趣的:(iOS基础之控件(篇2))