Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义视图don';通过在序列图像板中添加UIView以从自定义类继承,无法显示_Ios_Swift_Uiview_Custom View - Fatal编程技术网

Ios 自定义视图don';通过在序列图像板中添加UIView以从自定义类继承,无法显示

Ios 自定义视图don';通过在序列图像板中添加UIView以从自定义类继承,无法显示,ios,swift,uiview,custom-view,Ios,Swift,Uiview,Custom View,我用MyCustomView.xib编写了一个名为MyCustomView.swift的自定义视图类。 当我使用 let customView = MyCustomView(frame:.....) self.view.addSubView(customView) 将显示自定义视图 但当我直接在故事板中添加UIView,并选择MyCustomView作为自定义类的类时,它不会显示。即使我设置了UIView的背景色,它也不起作用。特别是当我用@IBDesignable注释MyCustomview

我用MyCustomView.xib编写了一个名为MyCustomView.swift的自定义视图类。 当我使用

let customView = MyCustomView(frame:.....)
self.view.addSubView(customView)
将显示自定义视图

但当我直接在故事板中添加UIView,并选择MyCustomView作为自定义类的类时,它不会显示。即使我设置了UIView的背景色,它也不起作用。特别是当我用@IBDesignable注释MyCustomview时,显示了两个错误警告

Failed to render and update auto layout status fo XXXViewController
Failed to update auto layout status
这是否意味着我应该在故事板中为自定义视图设置自动布局

这是MyCustomView.class

import UIKit
@IBDesignable
class MyCustomView:UIView{
@IBOutlet weak var categoryLabel: UILabel!

var category:String = ""{
    didSet{
        self.categoryLabel.text = category
    }
}
var intro:String = ""
var address:String = ""
var distance:CGFloat = 0.0


override init(frame: CGRect) {
    super.init(frame: frame)
    awakeFromNib()
    print("\(#function)")
}
convenience init(){
    print("\(#function)")
    self.init(frame:CGRect.zero)
}

required init?(coder aDecoder: NSCoder) {
    print("\(#function)")
    super.init(coder: aDecoder)
    awakeFromNib()

}

override func awakeFromNib() {
    super.awakeFromNib()
    let view = Bundle.main.loadNibNamed("MyCustomView", owner: self, options: nil)?.first as! UIView
    view.autoresizingMask = UIViewAutoresizing.flexibleWidth.union(.flexibleHeight)
    addSubview(view)
}
override func layoutSubviews() {
    super.layoutSubviews()
}

}

尝试删除
@IBDesignable
,因为Xcode正在类中查找不存在的
IBInspectable
属性

您需要做的是在
var
之前加前缀
@IBInspectable

下面是一个示例:自定义类:Rainbow.swift

import Foundation
import UIKit

@IBDesignable
class Rainbow: UIView {
    @IBInspectable var firstColor: UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
    @IBInspectable var secondColor: UIColor = UIColor(red: (171.0/255.0), green: (250.0/255), blue: (81.0/255.0), alpha: 1.0)
    @IBInspectable var thirdColor: UIColor = UIColor(red: (238.0/255.0), green: (32.0/255), blue: (53.0/255.0), alpha: 1.0)
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    func addOval(_ lineWidth: CGFloat, path: CGPath, strokeStart: CGFloat, strokeEnd: CGFloat, strokeColor: UIColor, fillColor: UIColor, shadowRadius: CGFloat, shadowOpacity: Float, shadowOffsset: CGSize) {
        
        let arc = CAShapeLayer()
        arc.lineWidth = lineWidth
        arc.path = path
        arc.strokeStart = strokeStart
        arc.strokeEnd = strokeEnd
        arc.strokeColor = strokeColor.cgColor
        arc.fillColor = fillColor.cgColor
        arc.shadowColor = UIColor.black.cgColor
        arc.shadowRadius = shadowRadius
        arc.shadowOpacity = shadowOpacity
        arc.shadowOffset = shadowOffsset
        layer.addSublayer(arc)
    }
    
    override func draw(_ rect: CGRect) {
        // Add ARCs
        self.addCirle(80, capRadius: 20, color: self.firstColor)
        self.addCirle(150, capRadius: 20, color: self.secondColor)
        self.addCirle(215, capRadius: 20, color: self.thirdColor)
    }
    
    func addCirle(_ arcRadius: CGFloat, capRadius: CGFloat, color: UIColor) {
        let X = self.bounds.midX
        let Y = self.bounds.midY
        
        // Bottom Oval
        let pathBottom = UIBezierPath(ovalIn: CGRect(x: (X - (arcRadius/2)), y: (Y - (arcRadius/2)), width: arcRadius, height: arcRadius)).cgPath
        self.addOval(20.0, path: pathBottom, strokeStart: 0, strokeEnd: 0.5, strokeColor: color, fillColor: UIColor.clear, shadowRadius: 0, shadowOpacity: 0, shadowOffsset: CGSize.zero)
        
        // Middle Cap
        let pathMiddle = UIBezierPath(ovalIn: CGRect(x: (X - (capRadius/2)) - (arcRadius/2), y: (Y - (capRadius/2)), width: capRadius, height: capRadius)).cgPath
        self.addOval(0.0, path: pathMiddle, strokeStart: 0, strokeEnd: 1.0, strokeColor: color, fillColor: color, shadowRadius: 5.0, shadowOpacity: 0.5, shadowOffsset: CGSize.zero)
        
        // Top Oval
        let pathTop = UIBezierPath(ovalIn: CGRect(x: (X - (arcRadius/2)), y: (Y - (arcRadius/2)), width: arcRadius, height: arcRadius)).cgPath
        self.addOval(20.0, path: pathTop, strokeStart: 0.5, strokeEnd: 1.0, strokeColor: color, fillColor: UIColor.clear, shadowRadius: 0, shadowOpacity: 0, shadowOffsset: CGSize.zero)
        
    }
}
它吸引

资料来源:

请尝试添加以下代码:


请为您的
MyCustomView
类添加代码。如何添加xib文件?@hafiz这是my MyCustomView.classcode@Leo我试过了……你是对的。但这不是我的重点。如果没有\@IBDesignable,脚本中的uiview仍然无法显示。我认为\@IBDesignable只是在另一个方面显示了我在使用自定义视图时的错误。但是Viewcontroller视图中的by代码运行良好@周雪静 但是删除
无法渲染和更新自动布局
右侧。添加
IBInspectable
属性可能会将其呈现到情节提要中。错误不会消失,但视图不会消失。我尝试使用代码在视图中绘制。它起作用了。但是锡伯没有感谢你!但是错误消失了,但是UIView仍然不显示。只有通过ViewController中的代码才能绘制视图。当您通过代码绘制视图时,只有在运行项目时才能看到效果。单击自定义视图对象,然后在Identity inspector中将**模块**设置为
IBDesignable
,如图所示,但IBDesignable是否只是该项目中的一个模块或组?我尝试了彩虹,效果很好。但我的观点是从锡伯觉醒过来的,它是行不通的
override func awakeFromNib() {
        super.awakeFromNib()
        xibSetup()
    }

    func xibSetup() {
        guard let view = loadViewFromNib() else { return }
        view.frame = bounds
        view.autoresizingMask = 
                    [.flexibleWidth, .flexibleHeight]
        addSubview(view)
        contentView = view
    }

    func loadViewFromNib() -> UIView? {
        guard let nibName = nibName else { return nil }
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName, bundle: bundle)
        return nib.instantiate(
                    withOwner: self, 
                    options: nil).first as? UIView
    }