簡體   English   中英

我在xcode的viewcontroller.swift中的一個按鈕中編碼 - 如何讓它在故事板中顯示?

[英]I coded in a button in xcode's viewcontroller.swift - How do I get it to show in storyboard?

具體來說,我在facebook的登錄按鈕中編碼到我的視圖控制器文件中。 但它沒有出現在故事​​板中。 我知道如何在故事板中創建一個按鈕,然后通過右鍵拖動連接代碼,但是當我嘗試將其連接到兩個並創建我的IBOutlet時,我似乎只是創建了兩個不同的登錄按鈕。

如何讓facebook登錄按鈕顯示在故事板中?

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit

class ViewController: UIViewController, FBSDKLoginButtonDelegate
{    
    override func viewDidLoad() {
        super.viewDidLoad()

        if (FBSDKAccessToken.currentAccessToken() == nil)
        {
            print("Not logged in..")
        }
        else
        {
            print("Logged in..")
        }

        let loginButton = FBSDKLoginButton()
        loginButton.readPermissions = ["public_profile", "email", "user_friends"]
        loginButton.center = self.view.center
        loginButton.delegate = self
        self.view.addSubview(loginButton)
    }

在這種情況下,您無法在故事板中看到該按鈕。 FBSDKLoginButton是Facebook提供的自定義按鈕。 通過使用FBSDKLoginButton,您可以繞過一些額外的代碼。 可以在故事板中添加自定義登錄按鈕,並使用按鈕的連接操作中的Facebook開發人員文檔進行編碼。

丹L是對的。 您只能操縱和查看通過故事板本身添加的Storyboard中的對象。 但是,通過在View Debugger中查看,您可以看到以編程方式添加到ViewControllerUIButton 這樣,您可以旋轉,放大和聚焦可見屏幕上的任何元素,甚至是Facebook按鈕。 但是這不允許您操縱視圖調試器中的對象。 大多數情況下,您希望這樣做來設置與屏幕上其他內容相關的自動布局約束。 你實際上仍然可以做到這一點:但它需要一點想象力。

1)您必須在視圖控制器的故事板( IBOutlets )中選擇要將按鈕錨定到的元素。

2) 在代碼中精神上可視化你想要UIButton去的地方。 對於按鈕程序的每個方向 ,將NSLayoutConstraints添加到周圍的視圖中。

UIView *leftView = self.viewToTheLeftOfWhereIWantTheButtonToGo; //IBOutlet. Remember to list here all of the surrounding views like so
UIView *button = self.theFacebookButton; //IBOutlet
//Top | do for each direction
[container addConstraint:[NSLayoutConstraint constraintWithItem:button
                                                      attribute:NSLayoutAttributeLeft  //button's Left
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:leftView
                                                      attribute:NSLayoutAttributeRight  //leftView's Right
                                                     multiplier:1.0
                                                       constant:kFacebookButtonPadding]]; //some CGFloat you define

// And do this for Left, Bottom, and Right also.

3)因為所有的子視圖,包括Facebook的按鈕是子視圖的self.view ,告訴主視圖布局它的子視圖。

[self.view layoutIfNeeded];

4)根據您的需要調整它,構建並運行,並在View Debugger中檢查您的結果。 這是使用故事板的下一個最好的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM