自定义子视图未在xib中加载

时间:2016-09-28 06:57:48

标签: ios swift xib

我正在尝试在文本字段内创建一个按钮,所以我创建了一个按钮并在xibSetup()中调用该函数。在运行程序时,函数被调用。但是没有在App中加载。卡在里面。

class NormalLogin: UIView {

weak var delegate:NormalLoginDelegate?
@IBOutlet weak var passwordField: UnderLinedTextField!
var view: UIView!
func showHideButton() {
    let btn = UIButton(frame: CGRect(x: self.frame.width - 70 , y: self.frame.size.height - 20 , width: 50, height: 20))
    btn.backgroundColor = UIColor.blackColor()
    btn.layer.borderWidth = 5
    btn.layer.borderColor = UIColor.blackColor().CGColor
    btn.setTitle("Click Me", forState: UIControlState.Normal)
    self.passwordField.addSubview(btn) // add to view as subview   
}

override init(frame: CGRect) {
    super.init(frame: frame)
    xibSetup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    xibSetup()
}

func xibSetup() {
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
    showHideButton() // Custom button i made
    addSubview(view)
}

func loadViewFromNib() -> UIView {
    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "NormalLogin", bundle: bundle)
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    return view
}

}

我做错了吗?

1 个答案:

答案 0 :(得分:1)

我猜它正在发生,因为你给错误的框架按钮

试试这个

let btn = UIButton(frame: CGRect(x: self.passwordField.frame.width - 70 , y: self.passwordField.frame.height - 20 , width: 50, height: 20))