gpt4 book ai didi

ios - 自定义 View 中的图像不会实时渲染

转载 作者:行者123 更新时间:2023-11-28 08:07:13 24 4
gpt4 key购买 nike

我正在为导航栏之类的东西构建一个自定义 UIView,并希望在我的 Storyboard 中的 View Controller 内显示此栏。酒吧分为两部分。一个 UIView 用于背景,一个 UIImageView 用于波浪图像。我的问题是,如果我在 Storyboard 中使用该自定义 View ,则只有背景 UIView 会实时渲染,而图像不会。如果我为图像设置一个 @IBInspectable 并将其设置为检查器中的 UIImageView,它就可以工作。

这是酒吧的样子:

Bar1

这就是它在 View Controller 中的样子: Bar2

这是我的代码(Swift 3):

TopBar.swift

@IBDesignable
class TopBar: UIView {

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

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

func initView() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "TopBar", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView

view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

addSubview(view)
}
}

最佳答案

尝试实现您的 awakeFromNib 方法并添加 prepareForInterfaceBuilder 方法

override func awakeFromNib() {
super.awakeFromNib()
self.initView()
}

override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.initView()
}

已编辑

这很奇怪,因为我一直在使用这个类,并且在我的 Storyboard中渲染得很好

import UIKit

@IBDesignable class GenericXibView: UIView {

@IBInspectable var nibName:String?
/**
View name.
*/
func getNibName() ->String?
{
return nibName
}

/**
View itself.
*/
var vView : UIView!;

override func awakeFromNib() {
super.awakeFromNib()
self.setup()
}

/**
Class construct. Initialize the view.
*/
public override init(frame oFrame: CGRect) {

super.init(frame: oFrame);
self.setup();
}

/**
Class construct. Initialize the view.
*/
public required init?(coder oDecoder: NSCoder) {

super.init(coder: oDecoder);
self.setup();
}

/**
Initialize the custom view and add it to this.
*/
fileprivate func setup() {

assert(self.getNibName() != nil, "View with empty nib name can't be instatiated")

self.vView = UINib(nibName: self.getNibName()!, bundle: Bundle(for: type(of: self))).instantiate(withOwner: self, options: nil)[0] as! UIView;

self.vView.translatesAutoresizingMaskIntoConstraints = false;

self.addSubview(vView);

self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":self.vView]));
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":self.vView]));

// self.vImgUser.layer.cornerRadius = (self.vImgUser.frame.size.width / 2);
// self.vImgUser.layer.masksToBounds = true;
self.setNeedsLayout();
}

override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setup()
vView.prepareForInterfaceBuilder()
}

/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/

}

还要确保你的类在你的 xib 文件中设置为 fileOwner

希望对你有帮助

关于ios - 自定义 View 中的图像不会实时渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44778049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com