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

标签 ios swift uiview swift3 xcode8

我正在为导航栏之类的东西构建一个自定义 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/

相关文章:

swift - 如何剪切长文本直到它适合 NSTextView 并在剪切文本的末尾添加 ...?

objective-c - 如何在两个 UIView 之间切换?

ios - UIView 的 shadowpath 颜色没有改变

ios - 如何在 LLDB for iOS 中找到堆栈跟踪的地址

ios - 为什么我的 UITableView 不使用单元格原型(prototype)?

ios - 如何让控制台更新当前正在播放的歌曲?

ios - 代码 8 : Interface builder shows different color that set

ios - 导入静态库后,header中有些header找不到

ios - react 原生/iOS SDK。 iOS SDK 更新到 14.5 后调用 'RCTBridgeModuleNameForClass' 没有匹配函数

swift - swift 协议(protocol)中的继承