在ViewController的中心添加UIView |自动版式

问题描述 投票:0回答:1

因此,我创建了一个进度指示器视图,该视图在API调用中显示。我已经为其创建了一个自定义UIView类。

现在,一切正常。但是视图的位置应该居中,但不是。

我认为我有正确的约束条件,但仍然无法正常工作。

这是我的代码:

import Foundation
import UIKit
import UICircularProgressRing
import HGRippleRadarView


class ProgressIndicator : UIView {

    @IBOutlet weak var contentView : UIView!
    @IBOutlet weak var progressView : UICircularProgressRing!
    @IBOutlet weak var logoContainerView : UIView!
    @IBOutlet weak var rippleView : RippleView!

    static let shared = ProgressIndicator()

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

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

    func commonInit() {
        Bundle.main.loadNibNamed("ProgressIndicator", owner: self, options: nil)
        addSubview(contentView)

    }

    public func show(controller : UIViewController) {
        setupLoadingView(controller : controller)
    }

    public func hide() {
        removeLoadingView()
    }

    private func setupLoadingView(controller : UIViewController) {

        controller.view.addSubview(self)

        // adding contrints on main view
        let leadingConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: controller.view, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)

        let trailingConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: controller.view, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)

        let topConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: controller.view, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)

        let bottomConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: controller.view, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)

        controller.view.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])

        // adding constraints on content view

        let leadingConstraint1 = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)

        let trailingConstraint1 = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)

        let topConstraint1 = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)

        let bottomConstraint1 = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)

        self.addConstraints([leadingConstraint1, trailingConstraint1, topConstraint1, bottomConstraint1])


        self.setNeedsLayout()
        self.reloadInputViews()
        self.layoutIfNeeded()


    }

}

这里是得到的结果:

enter image description here

这是xib文件的屏幕截图

enter image description here

ios swift uiview
1个回答
0
投票

您已添加约束,但未将translatesAutoresizingMaskIntoConstraints设置为false

self.translatesAutoresizingMaskIntoConstraints = false
self.contentView.translatesAutoresizingMaskIntoConstraints = false
© www.soinside.com 2019 - 2024. All rights reserved.