我正在使用xib
的两个UIView
文件。我想在Custominfoview
的第一个xib
视图中按下按钮时显示FirstView
。在这里,我很少感到困惑。我的代码在下面,请告诉我该怎么做。我已经搜索了很多,但没有找到。我已经在我想要的链接中附加了一张图片。
class CustominfoView: UIView {
@IBOutlet var mainCustomView: UIView!
@IBOutlet weak var infotextLbl: UILabel!
// MARK:-
// MARK:- Methods
// MARK: UIView Methods
required init(coder aDecoder : NSCoder) {
super.init(coder :aDecoder)!
setup()
}
override init(frame : CGRect) {
super.init(frame: frame)
setup()
}
func setup() {
let view = loadViewFromXib()
view.frame = bounds
// view.autoresizingMask = UIViewAutoresizing.flexibleWidth | UIViewAutoresizing.flexibleHeight
view.layer.cornerRadius = 12
view.layer.borderColor = UIColor.red.cgColor
view.layer.borderWidth = 2
addSubview(view)
}
func loadViewFromXib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "CustominfoView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
}
class FirstView: UIView {
@IBOutlet var mainCustomView: UIView!
@IBOutlet weak var infotextLbl: UILabel!
// MARK:-
// MARK:- Methods
// MARK: UIView Methods
required init(coder aDecoder : NSCoder) {
super.init(coder :aDecoder)!
setup()
}
override init(frame : CGRect) {
super.init(frame: frame)
setup()
}
func setup() {
let view = loadViewFromXib()
view.frame = bounds
// view.autoresizingMask = UIViewAutoresizing.flexibleWidth | UIViewAutoresizing.flexibleHeight
view.layer.cornerRadius = 12
view.layer.borderColor = UIColor.red.cgColor
view.layer.borderWidth = 2
addSubview(view)
}
func loadViewFromXib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "CustominfoView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
@IBAction func infoBtnpressed(_ sender: UIButton) {
print("Pressed")
}
}
}
答案 0 :(得分:0)
只需使用addSubview将customInfoView作为子视图添加到第一个视图的顶部即可。
(按下按钮)
let customInfoView = CustomInfoView()
self.addSubview(customInfoView)
然后当您需要关闭它时,只需将其从父项中删除。
答案 1 :(得分:0)
您可以执行以下操作:
@IBAction func infoBtnpressed(_ sender: UIButton) {
print("Pressed")
let customInfoView = CustomInfoView(frame: CGRect(x: 100, y: 100, width: 200, height: 200) )
self.addSubView(customInfoView)
}
在CGRect
中根据需要传递帧值。另外,您应该将customInfoView
声明为global
,以便可以轻松将其删除。