2017-08-18 14 views
2

制約を使用してFacebookのログインボタン(スクリーンショットの中央上部)を[Register or Login Button]の下に表示しようとしていますが、使用する制約とその使用方法を確認してください。私はSwift 3の新機能ですが、まだプログラム的に制約を払っていないので、どんな援助も歓迎されるでしょう!最初はCGRectのxとyの位置を使ってボタンを配置しましたが、別のデバイスでボタンが同じ場所にないことに気付きました。今は制約を使用しようとしています。プログラムでもプログラムで作成されたボタンでプログラムで制約を使用

それは次のようになります。

enter image description here

これが私のviewDidLoadでの私のボタンに関するコードです:

let verticalSpace = NSLayoutConstraint(
    item: self, attribute: .bottom, relatedBy: .equal, toItem: fbLoginButton, 
    attribute: .bottom, multiplier: 0.75, constant: 0) 

が、つながったことを:

let fbLoginButton = FBSDKLoginButton() 

view.addSubview(fbLoginButton) 

fbLoginButton.frame = CGRect(x: 0, y: 0, width: view.frame.width - 
     100, height: 40) 

self.view.addSubview(fbLoginButton) 

fbLoginButton.center.x = view.frame.width/2 

私がやってみました私はこのエラーを取得する:

私はボタンを追加した

Constraint items must each be an instance of UIView, or UILayoutGuide.

+1

を使用して追加された「ショーアラート」の下に、プログラムで「送信」 'item'は' self.view'でなく 'self'でなければなりません。エラーメッセージは' UIView'でなければならないと言っています。 'UIViewController'は' UIView'ではありません。また、 'fbLoginButton'で' translatesAutoresizingMaskIntoConstraints'を 'false'に設定してください – Paulw11

答えて

1

のtryコードだけボタンストーリーボード

ビューコントローラでこれをやっていると仮定すると、
@IBOutlet weak var showAlertButton: UIButton! 

func AddButton(){ 


    let sendButton = UIButton(type: .system) 
    sendButton.setTitle("Send", for: UIControlState()) 
    sendButton.translatesAutoresizingMaskIntoConstraints = false 
    self.view.addSubview(sendButton) 

    sendButton.leftAnchor.constraint(equalTo: showAlertButton.leftAnchor, constant: 0).isActive = true 
    sendButton.rightAnchor.constraint(equalTo: showAlertButton.rightAnchor, constant: 0).isActive = true 
    sendButton.topAnchor.constraint(equalTo: showAlertButton.bottomAnchor, constant: 10).isActive = true 
    sendButton.widthAnchor.constraint(equalToConstant: 80).isActive = true 
    sendButton.heightAnchor.constraint(equalTo: showAlertButton.heightAnchor).isActive = true 


} 

enter image description here

関連する問題