简体   繁体   中英

Subview's size not equal to its superview

Problem:

The yellow part is the subview( The subview is UIImageView, and the superview is UIView. ), its size is suppose to be the same as its superview (the gray part), what should I do to fix this?

Screen Images:

Here's the code:

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
}

Here are the constraints:

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

在此处输入图片说明

Your flash card view is UIView.And you want to make it full screen, then follow this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM