美文网首页
Swift分段选择控件(UISegmentedControl)的

Swift分段选择控件(UISegmentedControl)的

作者: MangoJ | 来源:发表于2016-06-27 13:32 被阅读131次

    import UIKit

    class ViewController: UIViewController        ,UITextFieldDelegate,UITextViewDelegate,UIActionSheet  Delegate,UIAlertViewDelegate{
    override func viewDidLoad() {
        super.viewDidLoad()
    
    
    
    let items = ["选项一","选项二",UIImage(named: "lalala")!] as [AnyObject]
        
        let segmented = UISegmentedControl (items: items)
        
        segmented.frame = CGRectMake(10, 270, 200, 30)
        
        segmented.selectedSegmentIndex = 1 //默认选中第二项
        
        segmented.addTarget(self, action:"segmentDidchange:", forControlEvents: UIControlEvents.ValueChanged)//添加值改变监听
    
        //添加选项
        segmented.insertSegmentWithTitle("新加项目", atIndex: 1, animated: true)
        //添加选项图片
        segmented.insertSegmentWithImage(UIImage(named: "lalala"), atIndex: 2, animated: true)
        
        //移除选项
        segmented.removeSegmentAtIndex(1, animated: true)
        
        
        segmented.tintColor = UIColor.redColor()
        //修改选项文字
        segmented.setTitle("swift", forSegmentAtIndex: 1)
        segmented.setTitle("学习", forSegmentAtIndex: 1)
      
        segmented.setImage(UIImage(named: "111"), forSegmentAtIndex: 3)
        //修改选项便宜位置
        segmented.setContentOffset(CGSizeMake(10, 7), forSegmentAtIndex: 0)
        self.view.addSubview(segmented)
    
    
      }
    
    
    func segmentDidchange(segmented:UISegmentedControl){
        //获取选项的索引
        print(segmented.selectedSegmentIndex)
        
        //获取选项的文字
        print(segmented.titleForSegmentAtIndex(segmented.selectedSegmentIndex))
    
     }
    
    }

    相关文章

      网友评论

          本文标题:Swift分段选择控件(UISegmentedControl)的

          本文链接:https://www.haomeiwen.com/subject/sapzdttx.html