如何以编程方式创建按钮?

swiftserver side programmingprogramming更新于 2025/5/2 21:37:17

在本文中,您将了解如何使用 Swift 语言以编程方式创建按钮。在 iOS 中,有多种以编程方式创建按钮对象的方法。让我们通过示例探索其中的一些方法。

我们将看到以下创建按钮的方法 -

步骤 1 - 在此步骤中,我们将创建一个按钮对象并在稍后需要时对其进行自定义。

步骤 2 - 在此步骤中,我们将使用惰性关键字创建一个按钮对象。

步骤 3 - 在此步骤中,我们将创建一个按钮对象并在稍后设置框架。

创建一个按钮并在稍后对其进行自定义

以下是使用 Swift 在 iOS 中以编程方式创建按钮的示例 -

示例

import UIKit
class TestController: UIViewController {    
   private let loginButton = UIButton()
   override func viewDidLoad() {
      super.viewDidLoad()
      initialSetup()
   }    
   private func initialSetup() {
      // 基本设置
      view.backgroundColor = .white
      navigationItem.title = "UIButton"
        
      // 登录按钮自定义
      loginButton.setTitle("Login", for: .normal)
      loginButton.setTitleColor(.white, for: .normal)
      loginButton.layer.cornerRadius = 10
      loginButton.layer.masksToBounds = true
      loginButton.backgroundColor = .darkGray
      loginButton.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
      loginButton.addTarget(self, action: #selector(handleLoginButtonTapped), for: .touchUpInside)
        
      // 向登录按钮添加约束
      view.addSubview(loginButton)
      loginButton.translatesAutoresizingMaskIntoConstraints = false
      loginButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
      loginButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
      loginButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
      loginButton.widthAnchor.constraint(equalToConstant: 280).isActive = true
   }    
   @objc private func handleLoginButtonTapped() {
      print("login button tapped...")
   }
}

输出

在上述步骤中,在控制器中创建了一个名为 loginButton 的按钮对象。按钮的标题设置为"Login",标题颜色设置为白色。按钮还被赋予了目标-动作对,这样当点击按钮时,将调用函数"handleLoginButtonTapped"。最后,按钮作为子视图添加到当前视图中。

您将看到,最初,我们创建了一个按钮对象。之后,我们将在 viewDidLoad() 方法中自定义按钮。最后,将其添加到具有一些必需约束的超级视图中。

使用 lazy 关键字创建按钮

以下是如何使用 UIKit 中的 lazy 关键字在 iOS 中以编程方式创建按钮的示例 -

示例

import UIKit
class TestController: UIViewController {   
   private lazy var loginButton: UIButton = {
      let button = UIButton()
      button.setTitle("Login", for: .normal)
      button.setTitleColor(.white, for: .normal)
      button.layer.cornerRadius = 10
      button.layer.masksToBounds = true
      button.backgroundColor = .darkGray
      button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
      button.addTarget(self, action: #selector(handleLoginButtonTapped), for: .touchUpInside)
      button.translatesAutoresizingMaskIntoConstraints = false
      return button
   }()    
   override func viewDidLoad() {
      super.viewDidLoad()
      initialSetup()
   }    
   private func initialSetup() {       
      // 基本设置
      view.backgroundColor = .white
      navigationItem.title = "UIButton"
        
      // 向登录按钮添加约束
      view.addSubview(loginButton)
      loginButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
      loginButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
      loginButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
      loginButton.widthAnchor.constraint(equalToConstant: 280).isActive = true
   }    
   @objc private func handleLoginButtonTapped() {
      print("login button tapped...")
   }
}

输出

在上述步骤中,使用 lazy 关键字在控制器中创建了一个名为 loginButton 的按钮对象。按钮的标题设置为"Login",标题颜色设置为白色。按钮还被赋予了目标-动作对,这样当点击按钮时,将调用函数"handleLoginButtonTapped"。最后,按钮作为子视图添加到当前视图。

请注意,使用 lazy 关键字,您不需要在按钮声明中将按钮添加为子视图,您可以在以后需要时执行此操作。

使用框架创建按钮

以下是使用 Swift 在 iOS 中以编程方式创建按钮的示例 -

示例

import UIKit
class TestController: UIViewController {    
   private let loginButton = UIButton(type: .system)    
   override func viewDidLoad() {
      super.viewDidLoad()
      initialSetup()
   }   
   private func initialSetup() {        
      // 基本设置
      view.backgroundColor = .white
      navigationItem.title = "UIButton"
        
      // 登录按钮自定义
      loginButton.frame = CGRect(x: 20, y: 150, width: 280, height: 50)
      loginButton.setTitle("Login", for: .normal)
      loginButton.setTitleColor(.white, for: .normal)
      loginButton.layer.cornerRadius = 10
      loginButton.layer.masksToBounds = true
      loginButton.backgroundColor = .darkGray
      loginButton.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
      loginButton.addTarget(self, action: #selector(handleLoginButtonTapped), for: .touchUpInside)
        
      view.addSubview(loginButton)
   }    
   @objc private func handleLoginButtonTapped() {
      print("login button tapped...")
   }
}

输出

您可以根据需要更改 x、y、宽度和高度,还可以根据需要更改按钮标题和操作。

结论

您可以通过多种方式以编程方式创建按钮。您可以创建一个按钮对象,并在以后需要时在代码中对其进行自定义。此外,可以使用 lazy 关键字来创建按钮对象。UIButton 类提供了在以后的代码中设置框架的灵活性。您可以在上面的示例中看到如何设置按钮的框架。

使用 lazy 关键字是最推荐的方式。您可以创建一个按钮对象并在单个块中添加任何自定义。此外,您还可以在同一个块中添加目标。



相关文章



关于我们 | 隐私条款 | 学习路线
返回顶部