iOS了解之UIPickerView、UIDatePicker、UISwitch、UISearchBar、UIStepper、UIActivityIndicatorView、UISegmente...

目录

    1. UIPickerView  数据选择器
    2. UIDatePicker  时间选择器
    3. UISwitch      开关
    4. UISearchBar   搜索
    5. UIStepper     步进器
    6. UIActivityIndicatorView   网络菊花
    7. UISegmentedControl        分段选择器
    8. UISlider                  滑块
    9. UIProgressView            进度条
    10. UIAlertController        弹框

1. UIPickerView(: UIView )数据选择器

// 创建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 numOfComs=[contentPickV numberOfComponents];
    // 获取 某组的行数
    NSInteger numOfRows=[contentPickV numberOfRowsInComponent:0];

    // 获取 某组的选中行数
    NSInteger selectedRow=[contentPickV selectedRowInComponent: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 时间选择器(: UIControl)

// 创建时间选择器
    //(一般使用继承UIPickView的自定义日期选择器,因为DatePickerMode局限性)
    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 开关 (: UIControl)

// 创建开关Switch(局限性:大小不能通过布局来修改,可使用形变、或者UIButton来代替)
    // 大小固定---可通过改变形变来改大小
    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 搜索 (: 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:@""];

    
    // 设置 是否显示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];

dele

    // <UISearchBarDelegate>
    [searchBar setDelegate:self];  


// 是否允许开始编辑
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{}
// 开始编辑后调用
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{}
// 是否允许结束编辑
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{}
// 结束编辑后调用
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{}
// 文本内容改变前调用(是否允许改变)
-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{return true;}
// 文本内容改变后调用
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{}

// 点击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 步进器( : UIControl)

// 
    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 网络菊花(: UIView)

//
    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 分段选择器(: UIControl)

//
    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 滑块(: UIControl)

//
    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 进度条(: UIView)

//
    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:^{
    }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容