UISegementControl切换简单应用

//  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))

    override func 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)

        }

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


猜你喜欢

转载自blog.csdn.net/catherine981234/article/details/77678249
今日推荐