【问题标题】:UIView not resizing from xib file SwiftUIView 没有从 xib 文件 Swift 调整大小
【发布时间】:2018-07-09 18:24:21
【问题描述】:

我创建了一个带有 UIView 的 xib 文件。我在情节提要中使用此视图,并希望它根据其中的内容调整大小。当我在情节提要中明确设置视图的高度时,它与 XIB 文件的高度相同。每个手机屏幕似乎也具有相同的 xib 高度和宽度,这导致视图离开屏幕,无论我在情节提要中设置什么约束。

我已经试过这个来隐藏视图并将横幅的高度设置为 35

self.specialistBanner.pillView.isHidden = true  
specialistBannerHeight.constant = 35

我试过了

specialistBannerHeight.constant = 35

让情节提要中的视图变小

我也试过了

self.specialistBanner.sizeToFit()

没有用

用于专家横幅视图的 XIB 文件

xib 文件内的标签

xib 中的药丸视图

在视图控制器中使用 xib 视图的故事板文件

class SpecialistBannerView: UIView {

@IBOutlet weak var bannerTitle: UILabel!
@IBOutlet weak var pillView: PillCollectionView!

var viewModel = SpecialistBannerViewModel()

// Initialize xib
let SpecialistBannerView = "SpecialistBannerView"

override func awakeFromNib() {
    super.awakeFromNib()
    self.translatesAutoresizingMaskIntoConstraints = false
}

override init(frame: CGRect) {
    super.init(frame: frame)
    initXib(view: self)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    initXib(view: self)
}

func initXib(view: SpecialistBannerView) {

    let viewToShow = Bundle.main.loadNibNamed(SpecialistBannerView, owner: self, options: nil)?[0] as? UIView ?? UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.sizeToFit()
    view.addSubview(viewToShow)
    view.frame = view.bounds
    view.pillView.setUp()
    view.sizeToFit()

    view.autoresizingMask = [.flexibleHeight, .flexibleWidth]

    viewModel.specialistBannerViewProtocol = self
}

任何帮助将不胜感激

【问题讨论】:

    标签: swift xcode uiview storyboard xib


    【解决方案1】:

    我遇到了同样的问题。我在另一个问题中找到了答案。 您必须在自定义视图中覆盖 LayoutSubViews() 并设置边界

    override func layoutSubviews() {
        super.layoutSubviews()
    
        // we need to adjust the frame of the subview to no longer match the size used
        // in the XIB file BUT the actual frame we got assinged from the superview
        self.view.frame = self.bounds
    }
    

    【讨论】:

    • 我在另一个问题中找到了答案 - 如果您在答案中链接该问题会更好。
    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2023-03-16
    • 2015-11-17
    • 1970-01-01
    • 2015-05-26
    相关资源
    最近更新 更多