swift - 将 subview 添加到堆栈 View 以设置背景颜色的函数或类

标签 swift xcode background-color subview uistackview

我正在创建一个输入表单,每个输入行(标签和文本字段)都是使用堆栈 View 排列的。我正在尝试向特定堆栈 View 添加白色背景颜色。我实现了它,但努力将其创建为函数或类,以便更轻松地控制哪些堆栈 View 获得背景。

关于将其转换为我可以在整个项目中使用的函数或类的任何提示? (注意:当前的解决方案,只为最后一个堆栈 View 添加背景颜色,而不是所有......还没有修复......)

 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

最佳答案

这是代码。您可以向任何堆栈 View 添加背景颜色。 您可以像这样向堆栈 View 添加颜色

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)
    }
}

关于swift - 将 subview 添加到堆栈 View 以设置背景颜色的函数或类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46755311/

相关文章:

css - 背景 VS 背景颜色 : performance

ios - 使用 Swift 解析 'Quick Start' - 'PFObject' 没有名为 'subscript' 的成员

ios - 调整约束时在代码中显示/不显示 Storyboard图像

ios - 好的做法是,可以在Info.Plist中添加注释,还是在iOS中添加权利?

CSS 样式用于 body 元素而不用于标题部分,当两者都配置时

java - 有没有办法将 java 文本区域中的每隔一行设置为不同的颜色?

swift - swift 中的可选链接和数组

ios - 如何在单击 mvvm 中的删除按钮时删除单元格

objective-c - 2 目标 1 图标名称

iphone - 购买iOS源代码的网站?