How to add the UISegmentedControl in UINavigationBar?

前端 未结 6 1370
遥遥无期
遥遥无期 2021-02-04 03:44

I have tried to add the UISegmentedControl to the bottom of UINavigationBar with title. But i cannot add it and I cannot add UISegmentedControl

6条回答
  •  春和景丽
    2021-02-04 04:34

    To add segment control in UINavigationBar in Swift 5

        let segment: UISegmentedControl = UISegmentedControl(items: ["First", "Second"])
        segment.sizeToFit()
        if #available(iOS 13.0, *) {
            segment.selectedSegmentTintColor = UIColor.red
        } else {
           segment.tintColor = UIColor.red
        }
        segment.selectedSegmentIndex = 0
        segment.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "ProximaNova-Light", size: 15)!], for: .normal)
        self.navigationItem.titleView = segment
    

提交回复
热议问题