UISegementControl切换简单应用

来源:互联网 发布:黑白棋估值函数优化 编辑:程序博客网 时间:2024/06/08 19:33

//  segement

//

//  Created by Catherine on 2017/8/29.

//  Copyright © 2017 Catherine. All rights reserved.

//


import UIKit


class ViewController: UIViewController {

    

    let viewBlue:UIView =UIView(frame: CGRect(x:0, y: 0, width:320, height: 568))

    let viewRed:UIView =UIView(frame: CGRect(x:0, y: 0, width:320, height: 568))

    overridefunc viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        let seg:UISegmentedControl =UISegmentedControl(items: ["blue","red"])

        seg.addTarget(self, action:#selector(change(seg:)), for:UIControlEvents.valueChanged)

        viewBlue.backgroundColor =UIColor.blue

        viewRed.backgroundColor =UIColor.red

        //在导航栏标题栏添加分页控制器

        self.navigationItem.titleView = seg

        

        //

        self.view.addSubview(viewBlue)

        self.view.addSubview(viewRed)

        //设置默认的那个一个view

        self.view.bringSubview(toFront:viewBlue)

    }

    func change(seg:UISegmentedControl){

        if seg.selectedSegmentIndex ==0{

            self.view.bringSubview(toFront:viewBlue)

        }else{

            self.view.bringSubview(toFront:viewRed)

        }

    }

    overridefunc didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


阅读全文
0 0
原创粉丝点击