2017-01-16 56 views
0

正確顯示我有以下代碼:FB登錄按鈕不在視野

override func viewDidLoad() { 
    super.viewDidLoad() 

    let fbLoginButton = FBSDKLoginButton() 
    fbLoginView.contentMode = UIViewContentMode.scaleAspectFit 
    fbLoginView.addSubview(fbLoginButton) 
    fbLoginButton.frame.origin = fbLoginView.frame.origin 

    fbLoginButton.frame = CGRect(x: fbLoginView.frame.origin.x, y: fbLoginView.frame.origin.y, width: fbLoginView.frame.width, height: fbLoginView.frame.height) 

    print("INFO width: \(fbLoginView.frame.width) height: \(fbLoginView.frame.height)") 
    print("INFO width: \(fbLoginButton.frame.width) height: \(fbLoginButton.frame.height)") 
    // fbLoginButton.delegate = self 
} 

我想一個小的子視圖,我在我的主視圖中添加Facebook登錄按鈕。出於某種原因,當我打印出來的UIView的寬度/高度,以及Facebook登錄按鈕,我看到這一點:

INFO寬度:1000.0高度:1000.0 INFO寬度:1000.0高度:1000.0

但是當我運行。在模擬器上我看到視圖心不是1000.0 X 1000.0(它會溢滿了屏幕,但它是不是應用它看起來像這樣:

enter image description here

您可以SE的藍色按鈕,並且它正確的在較小的子視圖內,但是,按鈕實際上似乎是溢出大於視圖。我不明白爲什麼視圖在模擬器中的尺寸是正確的,但它說它的尺寸非常大。我讀過這裏的帖子,告訴我調整按鈕的方向適合度,我已經玩過所有其他選項,但沒有運氣。

我有較小的UIView設置是這樣的: enter image description here

+0

是否使用自動版式? – Yannick

+0

是的,我在uiView @Yannick – mufc

回答

2

viewDidLoad,應用程序還沒有完成它的自動佈局經過這麼多的意見是1000x1000點。

因此,在您的代碼中,當您將loginButton的尺寸設置爲與loginView的尺寸相匹配時,它將複製1000的值。

稍後,當您的應用應用其自動佈局約束時,包含loginView的尺寸將調整爲正確的尺寸,但在其內部,loginButton的尺寸會更大。

正確的解決辦法是使用自動版式正確約束loginButton - 最簡單的變化將是內loginView添加約束它,因此它在同一時間調整:

NSLayoutConstraint(item: fbLoginButton, attribute: .Trailing, relatedBy: .Equal, toItem: fbLoginView, attribute: .Trailing, multiplier: 1.0, constant: 0.0).active = true 
NSLayoutConstraint(item: fbLoginButton, attribute: .Leading, relatedBy: .Equal, toItem: fbLoginView, attribute: .Leading, multiplier: 1.0, constant: 0.0).active = true 
NSLayoutConstraint(item: fbLoginButton, attribute: .Top, relatedBy: .Equal, toItem: fbLoginView, attribute: .Top, multiplier: 1.0, constant: 0.0).active = true 
NSLayoutConstraint(item: fbLoginButton, attribute: .Bottom, relatedBy: .Equal, toItem: fbLoginView, attribute: .Bottom, multiplier: 1.0, constant: 0.0).active = true 

或者它很可能是清潔劑添加您的故事板內Facebook的按鈕直接(添加一個空的視圖和它的類設置爲FBSDKLoginButton)。

+0

的尾部和前端都有一個約束43謝謝你的解決方案。我沒有意識到在viewDidLoad之後設置了自動佈局約束。我最終只是將views類設置爲FBSDKLoginButton,因爲它更簡單,並且完全符合我的需求。再次感謝! – mufc

+1

沒問題。 明確地說,約束條件是在'viewDidLoad'處設置的,但是它們不會被轉換成一個'frame',直到這個過程的晚些時候。正如Yannick指出的那樣,如果您需要在計算框架後調查某些子視圖的大小,您應該重寫'viewDidLayoutSubview'。 – Estel

+0

很好的解釋,爲什麼它不工作! – Yannick

0

在viewDidLoad中的幀的問題()是它們不layouted。他們還沒有他們的最終尺寸。你可以使用viewDidLayoutSubviews()。

我會建議使用NSLayoutConstraints,雖然。看看你想要用你的框架做什麼,你可以簡單地創建約束條件並將它們添加到你的視圖中。

let topConstraint = NSLayoutConstraint(item: fbLoginButton, attribute: .top, relatedBy: .equal, toItem: fbLoginView, attribute: .top, multiplier: 1, constant: 0) 
let bottomConstraint = NSLayoutConstraint(item: fbLoginButton, attribute: .bottom, relatedBy: .equal, toItem: fbLoginView, attribute: .bottom, multiplier: 1, constant: 0) 
let leadingConstraint = NSLayoutConstraint(item: fbLoginButton, attribute: .leading, relatedBy: .equal, toItem: fbLoginView, attribute: .leading, multiplier: 1, constant: 0) 
let trailingConstraint = NSLayoutConstraint(item: fbLoginButton, attribute: .trailing, relatedBy: .equal, toItem: fbLoginView, attribute: .trailing, multiplier: 1, constant: 0) 

self.view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])