2017-10-15 151 views
0

我創建一個輸入表單,它是每個輸入行(標籤和文本字段)使用堆棧視圖進行排列。我試圖添加一個白色的背景顏色到特定的堆棧視圖。我已經實現了它,但努力將其作爲一個函數或類來創建,以便更輕鬆地控制哪些堆棧視圖獲得背景。函數或類添加子視圖到堆棧視圖來設置背景顏色

將此轉換爲我可以在整個項目中使用的函數或類的任何提示? (注:目前的解決方案,不僅增加了背景顏色到最後的堆棧意見,而不是所有的..沒有固定的尚未..)

import UIKit 
import Firebase 

class createActivityVC: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // runs the functions to add backgrounds to the stackviews 

    pinBackground(backgroundView, to: nameStackView) 
    pinBackground(backgroundView, to: typeStackView) 
    pinBackground(backgroundView, to: weaponStackView) 
    pinBackground(backgroundView, to: killStackView) 
    pinBackground(backgroundView, to: sightingsStackView) 
    pinBackground(backgroundView, to: noteStackView) 

    // Do any additional setup after loading the view. 
} 

// Outlets used to set bacground color 
@IBOutlet weak var nameStackView: UIStackView! 
@IBOutlet weak var typeStackView: UIStackView! 
@IBOutlet weak var weaponStackView: UIStackView! 
@IBOutlet weak var killStackView: UIStackView! 
@IBOutlet weak var sightingsStackView: UIStackView! 
@IBOutlet weak var noteStackView: UIStackView! 

// Normal outlets 
@IBOutlet weak var name: UITextField! // required 
@IBOutlet weak var type: UITextField! // required - type of hunt 
@IBOutlet weak var weapon: UITextField! // required - weapon name 
@IBOutlet weak var kills: UITextField! // optional (eg. 1) 
@IBOutlet weak var sightings: UITextField! // optional (eg. 2) 
@IBOutlet weak var note: UITextField! // optional short description of the hunt 

@IBOutlet weak var saveBtn: UIButton! 
@IBOutlet weak var cancelBtn: UIButton! 



@IBAction func saveBtnWasPressed(_ sender: Any) { 
    if name.text != nil && type.text != nil && weapon.text != nil { 
     saveBtn.isEnabled = false // disabled the btn while uploading data - this ensure that user can't send the form twice 

     DataService.instance.uploadPost(uid: (Auth.auth().currentUser?.uid)!, name: name.text!, type: type.text!, weapon: weapon.text!, kills: kills.text!, sightings: sightings.text!, note: note.text!, withGroupKey: nil, sendComplete: { 
      (isComplete) in 
     if isComplete { 
      self.saveBtn.isEnabled = true 
      self.dismiss(animated: true, completion: nil) 

     } else { 
      self.saveBtn.isEnabled = true 
      print("There was an error with uploading the activity") 
     } 

     }) 


    } 

} 
@IBAction func cancelBtnWasPressed(_ sender: Any) { 
    self.dismiss(animated: true, completion: nil) 
} 


private lazy var backgroundView: UIView = { 
    let view = UIView() 
    view.backgroundColor = .white 
    return view 
}() 

private func pinBackground(_ view: UIView, to stackView: UIStackView) { 
    view.translatesAutoresizingMaskIntoConstraints = false 
    stackView.insertSubview(view, at: 0) 
    view.pin(to: stackView) 
} 

} 

public extension UIView { 
public func pin(to view: UIView) { 
    NSLayoutConstraint.activate([ 
     leadingAnchor.constraint(equalTo: view.leadingAnchor), 
     trailingAnchor.constraint(equalTo: view.trailingAnchor), 
     topAnchor.constraint(equalTo: view.topAnchor), 
     bottomAnchor.constraint(equalTo: view.bottomAnchor) 
     ]) 
} 
} 

這是現在的樣子.. screenshot

回答

0

這是代碼。你可以添加背景顏色到任何stackview。 您可以添加顏色以stackview等作爲

typeStackView.addBackground(color:.white) 



extension UIStackView { 

func addBackground(color: UIColor) { 
    let subView = UIView(frame: bounds) 
    subView.backgroundColor = color 
    subView.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
    insertSubview(subView, at: 0) 
} 

}

+0

檢查以下螺紋的更多細節。 https://stackoverflow.com/questions/34868344/how-to-change-the-background-color-of-uistackview – ahmed

相關問題