Why button changes after the click?(为什么点击后按钮会发生变化?)
问题描述
我想为 UIButton 设置自定义字体.我需要以编程方式设置它,因为字体不会以其他方式应用.问题是单击后它会变回内置字体.我做错了什么?
I want to set a custom font to UIButton. I need to set it programmatically because the font is not applied otherwise. The problem is that it changes back to the built-in font after the click. What am I doing wrong?
import UIKit
class LoginViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var eyeButton: UIButton!
@IBOutlet weak var loginButton: UIButton!
var iconClick = true
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
loginButton.titleLabel?.font = UIFont(name: "Capitana-Bold", size: CGFloat(16))
}
@IBAction func iconAction(sender: AnyObject) {
if (iconClick == true) {
passwordTextField.isSecureTextEntry = false
eyeButton.setImage(UIImage(named: "eye-closed"), for: .normal)
} else {
passwordTextField.isSecureTextEntry = true
eyeButton.setImage(UIImage(named: "eye-open"), for: .normal)
}
iconClick = !iconClick
}
@IBAction func onLoginClicked(_ sender: Any) {
}
}
推荐答案
这是一个 iOS 15 填充按钮.你不能说配置它
This is an iOS 15 Filled button. You cannot configure it by saying
loginButton.titleLabel?.font = ...
在 iOS 14 及之前的版本中这是可能的(但错误),但使用 iOS 15 按钮则不可能.要以编程方式配置填充按钮,您必须设置其配置对象.
That was possible (but wrong) in iOS 14 and before, but with an iOS 15 button it is impossible. To configure a Filled button programmatically, you must set its configuration object.
https://developer.apple.com/documentation/uikit/uibutton/configuration
特别是您要设置按钮配置属性标题.
In particular you want to set the button configuration attributed title.
https://developer.apple.com/documentation/uikit/uibutton/configuration/3750779-attributedtitle
这篇关于为什么点击后按钮会发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么点击后按钮会发生变化?
基础教程推荐
- Android Volley - 如何动画图像加载? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
