我正在尝试将一个xib文件两次添加到contentView,我想根据其中的内容大小更改contentView的高度。
此图显示了我想要实现的目标
这是我的代码
import UIKit
class ProfileSetViewController: UIViewController {
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var contentViewHeight: NSLayoutConstraint!
override func viewDidLayoutSubviews() {
if let CarViewAdd = NSBundle.mainBundle().loadNibNamed("CarViewAdd", owner: self, options: nil)[0] as? CarViewAdd {
contentViewHeight.constant = 220
contentView.frame.size.height = 220
CarViewAdd.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 220)
CarViewAdd.carImage.layer.cornerRadius = 10
CarViewAdd.carImage.clipsToBounds = true
CarViewAdd.carImage.layer.borderColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0).CGColor
CarViewAdd.carImage.layer.borderWidth = 3
contentView.addSubview(CarViewAdd)
}
if let CarViewAdd = NSBundle.mainBundle().loadNibNamed("CarViewAdd", owner: self, options: nil)[0] as? CarViewAdd {
contentViewHeight.constant = 442
contentView.frame.size.height = 442
CarViewAdd.frame = CGRectMake(0, 222, self.contentView.frame.size.width, 220)
CarViewAdd.carImage.layer.cornerRadius = 10
CarViewAdd.carImage.clipsToBounds = true
CarViewAdd.carImage.layer.borderColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0).CGColor
CarViewAdd.carImage.layer.borderWidth = 3
contentView.addSubview(CarViewAdd)
}
}
这是我得到的结果。
请注意顶部xib图片无法正确显示。我该如何解决这个问题?
答案 0 :(得分:1)
我通过为每个xib添加一个带有xib文件所需帧的UIView来解决问题,然后将xib添加到具有相同高度和宽度的UIView。
答案 1 :(得分:0)
尝试更改carImage.contentMode = .ScaleAspectFit