欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

【Swift】UISegmentedControl 实现分段控件

程序员文章站 2022-03-23 11:16:25
UISegmentedControl 详解,点击此处。具体实现:工程目录具体代码// AppDelegate.swiftimport UIKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunch...

UISegmentedControl 详解,点击此处

具体实现:

  • 工程目录

【Swift】UISegmentedControl 实现分段控件

  • 具体代码

//  AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        let homePage = HomePage()
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = homePage
        self.window?.backgroundColor = UIColor.white
        self.window?.makeKeyAndVisible()
        
        return true
    }

}

//  HomePage.swift

import UIKit

@objcMembers
class HomePage: UIViewController, UIScrollViewDelegate {
    
    let item1 = ViewController1()
    let item2 = ViewController2()
    let item3 = ViewController3()
    let item4 = ViewController4()
    
    var segmentView : UIScrollView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let screen = UIScreen.main.bounds
        
        // 定义分段控件
        let segmentItems = ["item1","item2","item3","item4"]
        let segmented = UISegmentedControl(items: segmentItems)
        segmented.selectedSegmentIndex = 0
        segmented.frame = CGRect(x: 0, y: 76, width: screen.size.width, height: 44)
        segmented.addTarget(self, action: #selector(HomePage.segmentChange(_:)), for: .valueChanged)
        self.view.addSubview(segmented)
        
        // 定义一个视图用于存放具体显示的内容
        self.segmentView = UIScrollView(frame: CGRect(x: 0.0, y: 100.0, width: screen.size.width, height: 500.0))
        self.segmentView.delegate = self
        self.view.addSubview(self.segmentView)
        
        
        self.item1.view.frame = CGRect(x: 0.0, y: 200.0, width: screen.size.width, height: 50.0)
        self.item2.view.frame = CGRect(x: 0.0, y: 200.0, width: screen.size.width, height: 50.0)
        self.item3.view.frame = CGRect(x: 0.0, y: 200.0, width: screen.size.width, height: 50.0)
        self.item4.view.frame = CGRect(x: 0.0, y: 200.0, width: screen.size.width, height: 50.0)
        
        
        // 初始化内容
        self.segmentView.addSubview(self.item1.view)
    }

    // segemnet选择改变事件
        func segmentChange(_ sender: AnyObject?)
        {
            let segment:UISegmentedControl = sender as! UISegmentedControl
            
            // 清空 删除segmentView的所有子视图
            for view in self.segmentView.subviews{
                view.removeFromSuperview()
            }

            switch segment.selectedSegmentIndex {

            case 0 :
                print("0 ")
                self.segmentView.addSubview(self.item1.view)
            case 1 :
                print("1 ")
                self.segmentView.addSubview(self.item2.view)
            case 2 :
                print("2 ")
                self.segmentView.addSubview(self.item3.view)
            case 3 :
                print("3 ")
                self.segmentView.addSubview(self.item4.view)
            default:
                print("default ")
            }

        }
    
}


//  ViewController1.swift

import UIKit

class ViewController1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let label = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 30.0))
        label.text = "content1"
        self.view.addSubview(label)
    }

}

// ViewController2.swift ViewController3.swift ViewController4.swift 与 ViewController1.swift基本相同在这里不再具体展示


分享一个觉得还不错的自定义分段控件,点击此处

本文地址:https://blog.csdn.net/AcongMiss/article/details/108570274

相关标签: swift ios segment