zoukankan      html  css  js  c++  java
  • Swift

    1,选择控件的创建,并监听控件选择值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
     
            //选项除了文字还可以是图片
            var items=["选项一","选项二",UIImage(named:"star")] as [AnyObject]
            var segmented=UISegmentedControl(items:items)
            segmented.center=self.view.center
            segmented.selectedSegmentIndex=1 //默认选中第二项      
            segmented.addTarget(self, action: "segmentDidchange:",
            forControlEvents: UIControlEvents.ValueChanged//添加值改变监听 
            self.view.addSubview(segmented)
        }
     
        func segmentDidchange(segmented:UISegmentedControl){
            //获得选项的索引
            println(segmented.selectedSegmentIndex)
            //获得选择的文字
            println(segmented.titleForSegmentAtIndex(segmented.selectedSegmentIndex))
        }
    }

    2,选择控件中选项的添加和删除

    1
    2
    3
    4
    5
    6
    //添加文字选项
    segmented.insertSegmentWithTitle("新增选项",atIndex:1,animated:true);
    //添加图片选项
    segmented.insertSegmentWithImage(UIImage(named:"icon")!,atIndex:1,animated: true)
    //移除选项
    segmented.removeSegmentAtIndex(1,animated:true);

    3,修改选项颜色(包括图片选项)

    1
    segmented.tintColor=UIColor.redColor()

    4,修改选项文字

    1
    segmented.setTitle("swfit",forSegmentAtIndex:1)

    5,修改选项图片

    1
    segmented.setImage(UIImage(named:"icon"),forSegmentAtIndex:2)

    6,修改选项内容偏移位置

    1
    segmented.setContentOffset(CGSizeMake(10,7),forSegmentAtIndex:1)
  • 相关阅读:
    力扣(LeetCode)605. 种花问题
    力扣(LeetCode)463. 岛屿的周长
    力扣(LeetCode)561. 数组拆分 I
    力扣(LeetCode) 263. 丑数
    区块链历史
    力扣(LeetCode) 821. 字符的最短距离
    力扣(LeetCode)804. 唯一摩尔斯密码词
    cmd 查看端口
    nginx windows版 下载和启动
    luogu P1270 “访问”美术馆
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4838154.html
Copyright © 2011-2022 走看看