【问题标题】:How to attach segmentControl to NavigationBar?如何将段控件附加到导航栏?
【发布时间】:2018-07-11 15:19:05
【问题描述】:

我尝试在导航栏中附加 UISegmentedControl,但出现问题:

分段控制:

func segmentControl() -> UIView {
let newUIView = UIView()

newUIView.backgroundColor = UIColor.clear
newUIView.layer.cornerRadius = 15.0

let items = ["list", "map"]
let customSC = UISegmentedControl(items: items)

customSC.layer.cornerRadius = 15.0
customSC.layer.borderWidth = 1
customSC.layer.borderColor = UIColor.green.cgColor

customSC.clipsToBounds = true
customSC.tintColor = UIColor.green

let font = UIFont.systemFont(ofSize: 16)
customSC.setTitleTextAttributes([NSAttributedStringKey.font: font],
                                for: .normal)

newUIView.addSubview(customSC)

return newUIView
}

我尝试附上:

SomeTab.navigationItem.titleView = segmentControl()

它应该看起来像:

【问题讨论】:

  • 有什么问题??
  • 段控制不显示。

标签: ios swift uikit uisegmentedcontrol


【解决方案1】:

试试这个代码

let segment: UISegmentedControl = UISegmentedControl(items: ["First", "Second"])
segment.sizeToFit()
segment.tintColor = UIColor(red:0.99, green:0.00, blue:0.25, alpha:1.00)
segment.selectedSegmentIndex = 0;
segment.setTitleTextAttributes([NSFontAttributeName: UIFont(name:"ProximaNova-Light", size: 15)!],
            forState: UIControlState.Normal)
self.navigationItem.titleView = segment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多