UISegmentControl分段控制器是UIKit框架提供的一组按钮栏,提供多个可选的按钮,只能激活其中的一个,响应事件。主要用来在同一层次重要性下不同的信息展示或者不同的界面展示之间切换。例如手机QQ的主界面的消息和电话两个选项卡。

我们看一下UISegmentedControl的继承结构:

UISegmentedControl-->UIControl-->UIView-->UIResponder-->NSObject

在iOS事件相关类中我们知道UIControl将触摸事件转换成了带接收方的控件事件,它本身是个抽象类,只能实例化它的子类来处理控件事件。UISegmentedControl继承自UIControl,它具有所有UIControl的属性和方法。

  • 属性

    @property(nonatomic) UISegmentedControlStyle segmentedControlStyle NS_DEPRECATED_IOS(2_0, 7_0, "The segmentedControlStyle property no longer has any effect") __TVOS_PROHIBITED;
    @property(nonatomic,getter=isMomentary) BOOL momentary; // if set, then we don't keep showing selected state after tracking ends. default is NO
    @property(nonatomic,readonly) NSUInteger numberOfSegments;
    @property(nonatomic) BOOL apportionsSegmentWidthsByContent NS_AVAILABLE_IOS(5_0);
    @property(nonatomic) NSInteger selectedSegmentIndex;
    @property(null_resettable,nonatomic,strong) UIColor *tintColor;

    iOS7之后采用扁平化设计风格,segmentedControlStyle被废弃,设置了也没有效果。momentary设置点击之后是否恢复原样,默认是NO。numberOfSegments表示选项卡的个数。apportionSegmentWidthsByContent默认为NO,如果设置为YES,对于宽度为0的segment它将根据其内容自动调整segment的宽度。selectedSegmentIndex表示当前选中的segment的索引。 tintColor描述View中线条轮廓的颜色,它默认会一直从最底部的View向上层传递,直到某个子View修改了tintColor,传递链断开,新的tintColor继续向上层传递。更多关于tintColorbackgroundColorforegroundColor的区别参考iOS字体颜色图片部分。

  • 对象方法

      - (instancetype)initWithItems:(nullable NSArray *)items; // items can be NSStrings or UIImages. control is automatically sized to fit content
    - (void)insertSegmentWithTitle:(nullable NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated; // insert before segment number. 0..#segments. value pinned
    - (void)insertSegmentWithImage:(nullable UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated;
    - (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
    - (void)removeAllSegments;
    - (void)setTitle:(nullable NSString *)title forSegmentAtIndex:(NSUInteger)segment; // can only have image or title, not both. must be 0..#segments - 1 (or ignored). default is nil
    - (nullable NSString *)titleForSegmentAtIndex:(NSUInteger)segment;
    - (void)setImage:(nullable UIImage *)image forSegmentAtIndex:(NSUInteger)segment; // can only have image or title, not both. must be 0..#segments - 1 (or ignored). default is nil
    - (nullable UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;
    - (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment; // set to 0.0 width to autosize. default is 0.0
    - (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
    - (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment; // adjust offset of image or text inside the segment. default is (0,0)
    - (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;
    - (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; // default is YES
    - (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;

    以上方法比较简单,根据方法名字就知道它的具体功能。需要注意的是initWithItems的数组参数既可以是字符串也可以是图片,segmentedControl会根据文字或者图片的宽高自动设置自己的宽高。setContentOffset设置图片或者文字内容相对于单个segment的坐标原点也就是左上角的偏移量,默认是(0,0).更多关于offset,inset的内容请参考iOS基本框架UIKit之几何形状setEnabled forSegmentAtIndex用来设置指定的索引的segment能否被选中。

  • 1.懒加载分段选择器,并且去掉分段选择器的选中蓝色渲染特效
//分段选择器
-(UISegmentedControl *)switchBtn{
if (!_switchBtn) {
_switchBtn=[[UISegmentedControl alloc]initWithItems:@[@"第1页",@"第2页",@"第3页"]];
[self.view addSubview:_switchBtn];
_switchBtn.backgroundColor=[UIColor whiteColor];
_switchBtn.layer.borderColor=[UIColor whiteColor].CGColor;
_switchBtn.tintColor=[UIColor whiteColor];
[_switchBtn addTarget:self action:@selector(actionSwitchBtn:) forControlEvents:UIControlEventValueChanged];
//设置分段选择器选中颜色
[_switchBtn setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
[_switchBtn setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor]} forState:UIControlStateSelected];
_switchBtn.selectedSegmentIndex=;
}
return _switchBtn;
}
  • 2.分段选择器与UIScrollView联动方法:
-(void)actionSwitchBtn:(UISegmentedControl*)segmentedControl{
self.scrollView.contentOffset=CGPointMake(self.view.frame.size.width*self.switchBtn.selectedSegmentIndex, );
}
#pragma -mark scrollview的代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
self.switchBtn.selectedSegmentIndex=self.scrollView.contentOffset.x/self.scrollView.frame.size.width;
}

UISegmentedControl去掉背景色与UIScrollView联动的更多相关文章

  1. UIColor用自定义颜色,TableView去掉背景色

    1.用mac系统自带的数码测色计,选RGB模式,将值添加到ColorWithRed:xxx.0/255 最后的alpha选1.0 2.TableView的背景色要用setBackgroundView的 ...

  2. 小程序BUTTON点击,去掉背景色

    添加hover-class <button form-type="submit"  hover-class="btn-hover"></but ...

  3. idea xml 绿背景色 去掉拼写检查

    去掉背景色 去掉拼写检查

  4. IOS 改变UISearchBar的背景色

    之前网上提供的方法试了很多种  都不能很好的去掉背景色  ,修改背景色方法如下: searchbar.barStyle = UIBarStyleBlackTranslucent; searchbar. ...

  5. iOS-UI-UI控件概述

    以下列举一些在开发中可能用得上的UI控件: IBAction和IBOutlet,UIView 1 @interface ViewController : UIViewController 2 3 @p ...

  6. iOS-UI控件概述

    IBAction和IBOutlet,UIView 1 @interface ViewController : UIViewController 2 3 @property(nonatomic, wea ...

  7. 不透明度opacity进阶

    一.opacity属性 1.opacity 习惯上说“透明度”,其实应该叫“不透明度”.opacity 意思:不透明,而背景色的默认值:transparent意思才是“透明的”.所以opacity用来 ...

  8. 移动端 几个css3属性的练习

    转行做前端,上班有一个星期了,什么都不懂,今天学习了几个新的css3属性,记录下来. 注:所有的测试均是在chrome上手机模式测试,与真实的手机环境可能有误差 1:-webkit-tap-heigh ...

  9. 【译】仿Taasky的3D翻转菜单动画实现

    最终效果 最终效果 开始 首先下载并打开一个事先搭好架子的Demo,然后来分析一下.这个Demo包含一个主页和详情页,其中MenuViewController继承自UITableViewControl ...

随机推荐

  1. linux 设备驱动程序中的一些关联性思考

    首先,个人感觉设备驱动程序与应用程序中的文件操作隔得有点远,用户空间不论是直接使用系统调用还是库函数都是通过系统调用的接口进入内核空间代码的.但是看过一个博客的分析整个过程,感觉中间层太过麻烦,必须经 ...

  2. Linux 进程等待队列【转】

    本文转载自:http://blog.csdn.net/dlutbrucezhang/article/details/9212067 Linux内核的等待队列是以双循环链表为基础数据结构,与进程调度机制 ...

  3. HDU3667 Transportation —— 最小费用流(费用与流量平方成正比)

    题目链接:https://vjudge.net/problem/HDU-3667 Transportation Time Limit: 2000/1000 MS (Java/Others)    Me ...

  4. 基于Vuejs的搜索匹配功能

    最近一直在看vue,查了很多资料,看了很多文档和博客,大概半知半解了,然后利用所理解的知识写了一个简单的搜索匹配功能. 大概长这个样子:   <!DOCTYPE html> <htm ...

  5. 一步一步学Silverlight 2系列(21):如何在Silverlight中调用JavaScript

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  6. I.MX6 按键开关机 PMIC 检测

    /************************************************************************* * I.MX6 按键开关机 PMIC 检测 * 说 ...

  7. codevs1258关路灯

    传送门 1258 关路灯  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master   题目描述 Description 多瑞卡得到了一份有趣而高薪的工作.每天早晨他 ...

  8. Java中gcRoot和引用类型

    看到一个老问题,Java是如何判定回收哪些对象的? 答:从gcRoot根搜索不可达,且标记清理一次之后仍没有被复活的对象,会被认定为垃圾对象进行清理.注意在Java中没有对象的作用域,只有对象的引用的 ...

  9. Laravel 5.4 中的异常处理器和HTTP异常处理实例教程

    错误和异常是处理程序开发中不可回避的议题,在本地开发中我们往往希望能捕获程序抛出的异常并将其显示打印出来,以便直观的知道程序在哪里出了问题并予以解决,而在线上环境我们不希望将程序错误或异常显示在浏览器 ...

  10. DC 兼容的DC

    DC是 "Device Content" , MS VC++ 的 MFC图形设备接口 的 设备描述表.它是MFC的主要对象之一.通过CDC类进行各种绘图操作,例如选笔,选色,选涂色 ...