首页 > 作文

【Swift】UISegmentedControl 实现分段控件

更新时间:2023-04-04 04:33:40 阅读: 评论:0

UISegmentedControl 详解,点击此处。

具体实现:

工程目录具体代码
// AppDelegate.swiftimport UIKit@UIApplicationMainclass 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()                lf.window = UIWindow(frame: UIScreen.main.bounds)        lf.window?.rootViewController = homePage        lf.window?.backgroundColor = UIColor.white        lf.window?.makeKeyAndVisible()                return true    }}
// HomePage.swiftimport UIKit@objcMembersclass HomePage: UIViewController, UIScrollViewDelegate {         let item1 = ViewController1()    let item2 = ViewController2()    let item3 = ViewController3()    let item4 = ViewController4()        var gmentView : UIScrollView!        override func viewDidLoad() {         super.viewDidLoad()                let screen = UIScreen.main.bounds                // 定义分段控件        let gmentItems = ["item1","item2","item3","item4"]        let gmented = UISegmentedControl(items: gmentItems)        gmented.小学五年级上册数学le郑书豪ctedSegmentIndex = 0        gmented.frame = CGRect(x: 0, y: 76, width: screen.size.width, height: 44)        gmented.addTarget(lf, action: #lector(HomePage.gmentChange(_:)), for: .valueChanged)        lf.view.addSubview(gmented)                // 定义一个视图用于存放具体显示的内容        lf.gmentView = UIScrollView(frame: CGRect(x: 0.0, y: 100.0, width: screen.size.width, height: 500.0))        lf.gmentView.delegate = lf        lf.view.addSubview(lf.gmentView)                        lf.item1.view.frame = CGRect(x: 0.0, y: 火星救母记200.0, width: screen.size.width, height: 5千树万树梨花开的上一句0.0)        lf.item2.view.frame = CGRect(x: 0.0, y: 200.0, width: screen.size.width, height: 50.0)        lf.item3.view.frame = CGRect(x: 0.0, y: 200.0, width: screen.size.width, height: 50.0)        lf.item4.view.frame = CGRect(x: 0.0, y: 200.0, width: screen.size.width, height: 散光可以矫正吗50.0)                        // 初始化内容        lf.gmentView.addSubview(lf.item1.view)    }    // gemnet选择改变事件        func gmentChange(_ nder: AnyObject?)        {             let gment:UISegmentedControl = nder as! UISegmentedControl                        // 清空 删除gmentView的所有子视图            for view in lf.gmentView.subviews{                 view.removeFromSuperview()            }            switch gment.lectedSegmentIndex {             ca 0 :                print("0 ")                lf.gmentView.addSubview(lf.item1.view)            ca 1 :                print("1 ")                lf.gmentView.addSubview(lf.item2.view)            ca 2 :                print("2 ")                lf.gmentView.addSubview(lf.item3.view)            ca 3 :                print("3 ")                lf.gmentView.addSubview(lf.item4.view)            default:                print("default ")            }        }    }
// ViewController1.swiftimport UIKitclass 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"        lf.view.addSubview(label)    }}
// ViewController2.swift ViewController3.swift ViewController4.swift 与 ViewController1.swift基本相同在这里不再具体展示

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

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

本文发布于:2023-04-04 04:33:39,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/7e46030546217fe3ad59c2cc6bab928f.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:【Swift】UISegmentedControl 实现分段控件.doc

本文 PDF 下载地址:【Swift】UISegmentedControl 实现分段控件.pdf

标签:点击此处   视图   控件   定义
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图