以编程方式将UIControl添加到Navbar标题中 - Swift

问题描述:

我正在尝试添加一个自定义UI分段控件,并将其创建到我的根视图控制器的导航栏中。这里是我的代码:以编程方式将UIControl添加到Navbar标题中 - Swift

分段控制:

@IBDesignable class FeedViewSC: UIControl { 


fileprivate var labels = [UILabel]() 
var thumbView = UIView() 

var items: [String] = ["Tab1", "Tab2"] { 
    didSet { 
     setupLabels() 
    } 

} 

var selectedIndex : Int = 0 { 
    didSet{ 
     displayNewSelectedIndex() 
    } 
} 

@IBInspectable var font : UIFont! = UIFont.systemFont(ofSize: 13) { 
    didSet { 
     setFont() 
    } 
} 

override init(frame: CGRect) { 
    super.init(frame: frame) 

    setupView() 
} 

required init?(coder: NSCoder) { 
    super.init(coder: coder) 
    setupView() 
} 

func setupView() { 

    layer.cornerRadius = 2 
    layer.borderColor = UIColor(red: 2/255, green: 239/255, blue: 23/255, alpha: 1).cgColor 

    backgroundColor = UIColor(red: 239/255, green: 29/255, blue: 239/255, alpha: 1) 

    setupLabels() 

    insertSubview(thumbView, at: 0) 

} 


func setupLabels() { 
    for label in labels { 
     label.removeFromSuperview() 
    } 

    labels.removeAll(keepingCapacity: true) 

    for index in 1...items.count { 
     let label = UILabel(frame: CGRect.zero) 
     label.text = items[index-1] 
     label.textAlignment = .center 
     label.font = UIFont(name: "timesnr",size: 17) 
     label.textColor = UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1) 
     self.addSubview(label) 
     labels.append(label) 
    } 
} 

override func layoutSubviews() { 
    super.layoutSubviews() 

    var selectFrame = self.bounds 
    let newWidth = selectFrame.width/CGFloat(items.count) 
    selectFrame.size.width = newWidth 
    thumbView.frame = selectFrame 
    thumbView.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1) 
    thumbView.layer.cornerRadius = 5 

    let labelHeight = self.bounds.height 
    let labelWidth = self.bounds.width/CGFloat(labels.count) 

    for index in 0...labels.count - 1 { 

     let label = labels[index] 

     let xPosition = CGFloat(index) * labelWidth 
     label.frame = CGRect(x: xPosition, y: 0, width: labelWidth, height: labelHeight) 
    } 

} 

override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { 
    let location = touch.location(in: self) 

    var calculatedIndex: Int? 
    for (index, item) in labels.enumerated() { 
     if item.frame.contains(location){ 
      calculatedIndex = index 
     } 
    } 
    if calculatedIndex != nil { 
     selectedIndex = calculatedIndex! 
     sendActions(for: .valueChanged) 
    } 

    return false 

} 


func displayNewSelectedIndex(){ 

    if(self.selectedIndex == -1){ 
     self.selectedIndex = self.items.count-1 
    } 

    let label = labels[selectedIndex] 
} 

func setFont(){ 
    for item in labels { 
     item.font = font 
    } 
} 

} 

我的VC,我会liek添加此分段控制到:

class FeedViewController: UIViewController { 


let feedViewSC: FeedViewSC = { 
    let sc = FeedViewSC() 
    sc.translatesAutoresizingMaskIntoConstraints = false 
    return sc 
}() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.view.backgroundColor = UIColor.white 
    view.addSubview(feedViewSC) 
    setupFeedViewSC() 
} 

func setupFeedViewSC() { 
    feedViewSC.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor, constant: 5).isActive = true 
    feedViewSC.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
    feedViewSC.heightAnchor.constraint(equalToConstant: 35).isActive = true 
    feedViewSC.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 60).isActive = true 
    feedViewSC.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -60).isActive = true 
} 

override func viewDidAppear(_ animated: Bool) { 
    let img = UIImage() 
    self.navigationController?.navigationBar.shadowImage = img 
    self.navigationController?.navigationBar.setBackgroundImage(img, for: UIBarMetrics.default) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

} 

如果你能告诉我怎么可以加我的自定义UIControl到我的视图控制器的导航栏标题。

+0

你也可以在故事板中做到这一点。拖动导航栏顶部的uview并将类名更改为FeedViewSC。您可以根据您的需要调整大小 – Vinodh

如果FeedViewController是NavigationController的初始视图控制器,你可以通过

let feedControl = FeedViewSC(frame: (self.navigationController?.navigationBar.bounds)!) 
feedControl.autoresizingMask = [.flexibleWidth,.flexibleHeight] 
self.navigationController?.navigationBar.addSubview(feedControl) 
feedControl.addTarget(self, action: #selector(FeedViewController.changingTab), for: .valueChanged) 

做到这一点很简单,至少我没有看到一个原因,这不会得到它在导航栏中工作。

也不是问题的一部分,但如果你有任何麻烦看到你在IB控制可能我建议。

override func prepareForInterfaceBuilder() { 
    super.prepareForInterfaceBuilder() 
    layer.cornerRadius = 2 
    layer.borderColor = UIColor(red: 2/255, green: 239/255, blue: 23/255, alpha: 1).cgColor 
    backgroundColor = UIColor(red: 239/255, green: 29/255, blue: 239/255, alpha: 1) 
    setupLabels() 
    insertSubview(thumbView, at: 0) 
} 

至于控制本身我没有测试它,但所有活动,您的处理可能比值略有不同变化,我不知道。

您也可以使控制器的导航栏成为一个特殊的可设计类,并且绝不会将其添加到代码中,但您可能必须在viewDidLoad中获得一个引用才能使用它。可设计看起来像

然后在你的控制器中说viewDidLoad你可以做到这一点。

if let navController = self.navigationController{ 
     if navController.navigationBar is DesignableNavBar{ 
      let control = (navController.navigationBar as! DesignableNavBar). feedControl 
      control?.addTarget(self, action: #selector(ViewController.changingTab), for: .valueChanged) 
     } 
    } 
+0

我知道您标记为正确我更新了它,如果您希望能够在导航控制器中看到它。只需更改uinavigationcontroller中的导航栏以匹配上面的类,那么它就可以在导航栏的故事板中设计。请享用 – agibson007