为什么点击后按钮会发生变化? [英] Why button changes after the click?

查看:27
本文介绍了为什么点击后按钮会发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 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

这篇关于为什么点击后按钮会发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆