2017-03-01 62 views
0

我正在嘗試添加一個自定義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

回答

0

如果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

相關問題