2016-09-28 5 views
0

ボタンを作成してxibSetup()でボタンを作成しました。プログラムの実行中に関数が呼び出されます。しかし、アプリケーションに読み込まれません。そこにはまった。カスタムサブビューがxibにロードされていません

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 
} 

}

私は何も悪いことをしましたか?

+0

を試してみてください、あなたはボタンフレームに与えているXとYのポイントは正しいですか? 私はそれがself.viewではないパスワードフィールドのフレームに従うべきだと思います。 –

答えて

1

あなたがボタンに

を間違ったフレームを与えているので、私はそれが起こっていると思いますが、この

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

passwordField:UnderLinedTextField!メンバーの幅/高さはありません。 – Joe

+0

が更新されました。私はちょうどボタンフレームがpasswordFieldによるべきであることを指摘していた。 – Sahil

関連する問題