• UISegmentedControl去掉背景色与UIScrollView联动


    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=0;
        }
        return _switchBtn;
    }
    • 2.分段选择器与UIScrollView联动方法:
    -(void)actionSwitchBtn:(UISegmentedControl*)segmentedControl{
        self.scrollView.contentOffset=CGPointMake(self.view.frame.size.width*self.switchBtn.selectedSegmentIndex, 0);
    }
    #pragma -mark scrollview的代理方法
    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        self.switchBtn.selectedSegmentIndex=self.scrollView.contentOffset.x/self.scrollView.frame.size.width;
    }
  • 相关阅读:
    2013 duilib入门简明教程 -- 自绘控件 (15)
    2013 duilib入门简明教程 -- 部分bug 2 (14)
    2013 duilib入门简明教程 -- 复杂控件介绍 (13)
    Linux学习88 Mysql常见服务搭档-php-fpm基础应用与实战
    Linux学习87 MariaDB高级操作实战
    Linux学习86 MariaDB基础操作实战
    Linux学习85 MariaDB入门实战
    Linux学习84 数据库体系全面介绍-关系型数据库基础
    Linux学习83 互联网架构-LAMP高级应用与企业项目
    Linux学习82 互联网架构-LAMP入门进阶
  • 原文地址:https://www.cnblogs.com/luerniu/p/5830434.html
Copyright © 2020-2023  润新知