1、首先自己先创建一个类,例如广告 Advertisement
required init?方法一定要实现 否则报错;
其中
@IBOutlet weak var advImageView: UIImageView! 广告图片ImageView声明,
@IBAction func onCloseButtonClick 为广告窗口关闭按钮监听
class AdvertisementView: UIView {
@IBOutlet weak var advImageView: UIImageView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBAction func onCloseButtonClick(_ sender: Any) {
self.removeFromSuperview();
}
}
2、 创建view的xib文件
xib的view记得要继承 Advertisement.swift类
91B138F3-3F22-427C-B24B-5DCBE3861185.png Advertisement.xib.png3、在相关页面使用此View
//声明advertisementView变量
var advertisementView:AdvertisementView!;
func initAdvertisementView(){
//加载xib中的view, Advertisement为xib文件名
advertisementView = Bundle.main.loadNibNamed("Advertisement", owner: nil, options: nil)?.first as! AdvertisementView
advertisementView.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
self.view.addSubview(advertisementView);
}
网友评论