xib中怎么将视图添加的uiimageview上

xib中怎么将视图添加的uiimageview上,第1张

先添加一个UIView,将这个UIView的class改成UIImageVIew,然后就可以添加控件到这个转换过的UIImageView了这个控件就和UIimageview形成了父子层级关系.

转换的UIImageView和直接添加的UIImageView不同在于:你不能直接在面板中设置UIImageView的image需要在viewDidload中 进行设置

swift用xib自定义View的方法如下:

1. 创建一个IDView,添加一个IDView.Xib

2. 对IDCard.xib添加约束

3、在IDCard.xib的 File's Owner class 设置为IDCard:

4、在IDCard.swift中添加如下代码,把xib的view连线到代码上的contentView:

5、绑定xib,实现 @IBInspectable, @IBDesignable这几部分代码

@IBDesignable

class IDCard: UIView {

   

   @IBOutlet var contentView: UIView!

   @IBInspectable

   var cornerRadius: CGFloat = 0 {

       didSet {

           layer.cornerRadius = cornerRadius

           layer.masksToBounds = cornerRadius >0

       }

   }

   @IBInspectable

   var borderWidth: CGFloat = 0 {

       didSet {

           layer.borderWidth = borderWidth

       }

   }

   @IBInspectable

   var borderColor: UIColor? {

       didSet {

           layer.borderColor = borderColor?.CGColor

       }

   }

   

   override init(frame: CGRect) {

       super.init(frame: frame)

       initialFromXib()

   }

   required init?(coder aDecoder: NSCoder) {

       super.init(coder: aDecoder)

       initialFromXib()

   }

   

   func initialFromXib() {

       let bundle = NSBundle(forClass: self.dynamicType)

       let nib = UINib(nibName: "IDCard", bundle: bundle)

       contentView = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

       contentView.frame = bounds

       addSubview(contentView)

       }

}

6、运行代码,结果如下


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11235255.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存