简体   繁体   English

我想以编程方式快速将按钮放置在另一个UI元素上方

[英]I want to place a button above another UI element programmatically in swift

I tried to place a button programmatically above a separator but it doesn't show at run time this is my view in story board shown in the picture 我试图以编程方式将按钮放置在分隔符上方,但在运行时并未显示,这是我在故事板上的视图 X

the separator is UIview I dragged and dropped it in storyboard and linked it to my controller 分隔符是UIview,我将其拖放到情节提要中并将其链接到我的控制器

this is my code // 这是我的代码//

import UIKit
import Firebase
import FBSDKCoreKit
import FBSDKLoginKit

class LoginController: UIViewController, FBSDKLoginButtonDelegate {

    @IBOutlet weak var separator: UIView!
    @IBOutlet var fbLoginButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        if FBSDKAccessToken.current() != nil {
            // Present the main view because user already logged in
            self.performSegue(withIdentifier: "goToHome", sender: self)
        }else{

            loginButtonUI()
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)

        loginButtonUI()
    }

    private func loginButtonUI(){

        let loginButton = FBSDKLoginButton()
        loginButton.delegate = self
        // Optional: Place the button in the center of your view.
        loginButton.center = view.center
        view.addSubview(loginButton)
    }

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {

        // fb login code

    }


}

but the button doesn't show at run time why? 但是按钮为什么在运行时不显示?

you need to activate your constraints, and make sure you added correctly 您需要激活约束,并确保添加正确

let loginButton = FBSDKLoginButton()
    loginButton.delegate = self

    view.addSubview(loginButton)
    loginButton.translatesAutoresizingMaskIntoConstraints = false
    loginButton.centerXAnchor.constraint(equalTo: separatorView.centerXAnchor).isActive = true
    loginButton.bottomAnchor.constraint(equalTo: separatorView.topAnchor, constant: 20).isActive = true
    loginButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    loginButton.heightAnchor.constraint(equalToConstant: 40).isActive = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift以编程方式将UIImageView放在另一个上面并调整大小 - Swift programmatically place UIImageView on top of another and resize 我想在 Swift UI 中使用带有按钮的 Popover,但是当有多个按钮时出现问题 - I want to use Popover with a button in Swift UI, but the problem when there are multiple buttons 通过 storyboard 或以编程方式将暗模式强制为 Swift 中的一个 UI 元素 - Force the Dark Mode to only one UI Element in Swift by storyboard or programmatically 在下一个“故事板”中按下按钮,在Swift中以编程方式设计UI - Button pressed to next “storyboard” Programmatically designing UI in Swift 如何在Swift中使用另一个UIViewController将容器覆盖父UIViewController中的UI元素? - How can I cover a UI element from the parent UIViewController by a container with another UIViewController in Swift? 以编程方式在图像上放置一个按钮 - Place a Button over an Image Programmatically 以编程方式收藏按钮 Swift - Favorite button programmatically Swift 以编程方式添加按钮 swift - Adding button programmatically swift 在scrollview中的每个元素上方放置标签? - Place label above each element in scrollview? 如何通过单击按钮在 Swift UI 中从一个视图导航到另一个视图 - How to navigate from one view to another in Swift UI on a click of button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM