子视图的大小不等于其超级视图

时间:2017-02-03 07:53:17

标签: ios swift

问题:

黄色部分是子视图(子视图是UIImageView,superview是UIView。),它的大小与其superview(灰色部分)相同,我应该怎么做要解决这个问题吗?

屏幕图片:

以下是代码:

var linkedMemory = Memory(masteryLevel: 1, algorithm: Algorithm.algorithm1.chooseAlgorithm(), forgetRatio: 0, lastStudyTime: Date(), front: #imageLiteral(resourceName: "Ideas-Yellow"), back: #imageLiteral(resourceName: "Ideas-Blue"))

var frontView: UIView {
    let front = showContent(of: linkedMemory.front)
    return front
}

convenience init(memory: Memory) {
    self.init(frame: CGRect())
    self.linkedMemory = memory
    self.setupView()
}

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

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

func setupView() {
    self.layer.cornerRadius = 20
    self.layer.shadowRadius = 12
    self.layer.shadowOpacity = 0.15
    self.layer.masksToBounds = true
    addGesture()
    sizeNAddSubview(view: frontView)
}

func sizeNAddSubview(view: UIView) {
    view.frame.size = self.bounds.size
    addSubview(view)
}

func showContent(of linkenMemory: Any) -> UIView {
    var contentView = UIView()
    if let content = linkenMemory as? UIImage {
        let imageView = UIImageView()
        imageView.image = content
        contentView = imageView
    }
    if let content = linkedMemory as? String {
        let label = UILabel()
        label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        label.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        let text = content
        label.text = text
        contentView = label
    }
    return contentView
}

以下是限制因素:

2 个答案:

答案 0 :(得分:1)

您可以将约束设置为相等的高度和相等的superview宽度 [要么]  如果您在布局通过之前设置此视图,则超级视图的大小将不是实际大小,因此您必须将代码移至viewDidAppear()viewDidLayoutSubviews()

答案 1 :(得分:0)

enter image description here

你的闪存卡视图是UIView.And你想全屏显示,然后按照这个。