ios - 在 Swift 中以编程方式添加自动布局约束将 uiview 变为黑色

标签 ios swift autolayout constraints

我有一个名为 DropDownList 的 UIView 类,其中包含一个标签和一个按钮。该标签和按钮放置在堆栈 View 中。以下是我添加到此 View 的约束。问题是,当添加此约束时,整个 View (主视图)在设备中变黑,但在模拟器中工作。当更改按钮标题时,我还面临另一个问题,整个 View (主视图)在模拟器中变黑。我正在使用 swift 4 和 Xcode 9.0。请帮忙...

class DropDownList: UIView {

    let DefaultSpace: CGFloat = 50.0
    let Margin: CGFloat = 10.0
    let StackViewSpacing: CGFloat  = 50.0

    var mDataList:[Dictionary<String,Any>] = [] {

    }
    var mFieldLabel: UILabel =
    {
        let fieldLabel = UILabel()
        fieldLabel.font = AppFont
        fieldLabel.textAlignment = .left
        return fieldLabel
    }()

    var mDropDown: DropDown = DropDown()
    var mDropDownBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("-Select-", for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.textAlignment = .left
        btn.titleLabel?.font = AppFont
        btn.addTarget(self, action:#selector(dropDownClick) , for: .touchUpInside)
        return btn

    }()

    lazy  var mStackView: UIStackView = {

        let stackView = UIStackView(arrangedSubviews: [mFieldLabel, mDropDownBtn])
        stackView.alignment = .center
        stackView.distribution = .fill
        stackView.axis = .horizontal
        stackView.spacing = StackViewSpacing
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.backgroundColor = .yellow
        return stackView
    }()

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    // MARK: - Init



    init(with data:[Dictionary<String,Any>], inView: UIView, parent: DropDownList?, fieldName: String , topSpacing: CGFloat )
    {
        super.init(frame: CGRect(x: 0, y: 40, width: 400, height: 50))
        setViews(fieldName: fieldName, data: data, inView: inView)
        self.setConstaintsWithTopSpacing(inView: inView,topSpace: topSpacing, parentView: inView)



    func setViews(fieldName: String, data:[Dictionary<String,Any>], inView: UIView)
    {
        self.translatesAutoresizingMaskIntoConstraints = false
        self.mDataList = data
        self.mFieldLabel.text = fieldName
        DropDown.startListeningToKeyboard()
        mDropDown.anchorView = mDropDownBtn
        self.addSubview(mStackView)
        inView.addSubview(self)

 mDropDown.selectionAction = { [unowned self] (index: Int, item: String) in
            print("Selected item: \(item) at index: \(index)")
            self.mDropDownBtn.setTitle(item, for: .normal)
            self.updateConstraints()
            self.layoutIfNeeded()
        }

    }





    // MARK: - Constraints

    func setConstaintsWithTopSpacing(inView: UIView, topSpace: CGFloat, parentView: UIView)
    {
        if (inView.translatesAutoresizingMaskIntoConstraints)
        {
            inView.translatesAutoresizingMaskIntoConstraints = false
        }


        let leadingConstraint = NSLayoutConstraint(item: self, attribute: .leading, relatedBy: NSLayoutRelation.equal, toItem: inView, attribute: .leading, multiplier: 1, constant: Margin)
        let topConstraint = NSLayoutConstraint(item: self, attribute: .top, relatedBy: NSLayoutRelation.equal, toItem: parentView, attribute: .top, multiplier: 1, constant: topSpace)
        let trailingConstraint = NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: NSLayoutRelation.equal, toItem: inView, attribute: .trailing, multiplier: 1, constant: Margin)
        let heightConstranit = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 40)



        inView.addConstraints([leadingConstraint, topConstraint, trailingConstraint, heightConstranit])
        setStackViewConstraints()
    }

    func setConstaintsWithTopLayout(inView: UIView, topLayout: UILayoutSupport?)
    {
        if (inView.translatesAutoresizingMaskIntoConstraints)
        {
            inView.translatesAutoresizingMaskIntoConstraints = false
        }

        setStackViewConstraints()


        let leadingConstraint = NSLayoutConstraint(item: self, attribute: .leading, relatedBy: NSLayoutRelation.equal, toItem: inView, attribute: .leading, multiplier: 1, constant: 10)
        let topConstraint = NSLayoutConstraint(item: self, attribute: .top, relatedBy: NSLayoutRelation.equal, toItem: inView, attribute: .top, multiplier: 1, constant: 80)
        let trailingConstraint = NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: NSLayoutRelation.equal, toItem: inView, attribute: .trailing, multiplier: 1, constant: 10)

        inView.addConstraints([leadingConstraint, topConstraint, trailingConstraint])





    }

    func  setStackViewConstraints()
    {



        let leadingConstraintStackView = NSLayoutConstraint(item: mStackView, attribute: .leading, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0)
        let topConstraintStackView = NSLayoutConstraint(item: mStackView, attribute: .top, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
        let trailingConstraintStackView = NSLayoutConstraint(item: mStackView, attribute: .trailing, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0)
        let bottomConstraintStackView = NSLayoutConstraint(item: mStackView, attribute: .bottom, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)

        self.addConstraints([leadingConstraintStackView, topConstraintStackView, trailingConstraintStackView, bottomConstraintStackView])
    }


}

我正在创建此类的对象和主视图的 subview 。

let sailsOfficeDl = DropDownList(with: [dic, dic1], inView: self.view, fieldName: "Sales Office", topLayout: self.topLayoutGuide)

最佳答案

当我创建 UIView 的子类时,我之前注意到了相同的行为

我们在初始化类时明确需要将背景颜色设置为 Clear

init(frame:CGRect,distance:CGFloat,width:CGFloat)
    {
        super.init(frame: frame)
        /// Here I need to set the BackGround color as Clear
        /// If I comment this line Output shown is output 1
        /// if I dont comment this Line Output is shown in output 2
        self.backgroundColor = UIColor.clear
        self.isUserInteractionEnabled = true
        self.distance = distance
        self.textBgColor = UIColor.red
        self.labelText = "Sample"
        self.bgColor = UIColor.black
        self.viewWidth = width
        self.textFontSize = 14
        self.frame = CGRect(x: 0, y: 0, width: (distance*2)+width, height: (distance*2)+width)
    }

输出 1 enter image description here

输出 2 enter image description here

在output-1中我的整个矩形变成黑色 在输出 2 中,只有我绘制的上下文是黑色,因为我将其设置为黑色

希望这对您有帮助

关于ios - 在 Swift 中以编程方式添加自动布局约束将 uiview 变为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49912665/

相关文章:

ios - 为所有设备屏幕设置UIImage覆盖屏幕,xib文件

ios - Xcode 错误 : Abort trap 6 (only show in device)

ios - Restore Purchase for iAP 在应用程序启动时显示警报

ios - 自定义 UITextField 类仅适用于第一个文本字段

ios - 如何获取 didSelectRowAt IndexPath 以区分部分?

ios - Autolayot 纵横比 iOS

ios - 始终在 iOS 上获取 In_app 的状态 SKPaymentTransactionStatePurchasing

ios - 我也可以像 xcassets 一样在源代码中切片图像资源吗?

json - 处理包含多种类型的 JSON 数组 - Swift 4 Decodable

ios - UITextView 总是滚动到内容的底部?