You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS开发:实现Stocks与Favorites按钮动态样式切换问题

解决方案

你的问题核心是**viewDidLoad仅在视图初始化时执行一次**,里面的判断逻辑无法响应按钮点击事件。要实现点击切换样式,需要给按钮绑定点击回调,在回调中动态更新UI样式。

步骤说明

  1. 将样式设置方法(activated/disabled)从viewDidLoad中移出,作为类的独立方法,方便点击事件调用。
  2. 给两个按钮添加点击事件监听,指定点击后触发的处理函数。
  3. 在点击处理函数中,切换按钮的选中状态,并根据状态更新对应样式。

完整代码示例

class YourViewController: UIViewController {
    // 定义按钮属性
    private let stocksButton = UIButton(type: .system)
    private let favoritesButton = UIButton(type: .system)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupButtons()
    }
    
    // 初始化按钮配置
    private func setupButtons() {
        // 设置标题
        stocksButton.setTitle("Stocks", for: .normal)
        favoritesButton.setTitle("Favorites", for: .normal)
        
        // 默认状态:Stocks激活,Favorites禁用
        stocksButton.isSelected = true
        activated(button: stocksButton)
        disabled(button: favoritesButton)
        
        // 绑定点击事件
        stocksButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
        favoritesButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
        
        // 添加到视图并设置AutoLayout布局
        stocksButton.translatesAutoresizingMaskIntoConstraints = false
        favoritesButton.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stocksButton)
        view.addSubview(favoritesButton)
        
        NSLayoutConstraint.activate([
            stocksButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
            stocksButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            favoritesButton.topAnchor.constraint(equalTo: stocksButton.topAnchor),
            favoritesButton.leadingAnchor.constraint(equalTo: stocksButton.trailingAnchor, constant: 20)
        ])
    }
    
    // 按钮点击处理
    @objc private func buttonTapped(_ sender: UIButton) {
        // 更新选中状态
        stocksButton.isSelected = sender == stocksButton
        favoritesButton.isSelected = sender == favoritesButton
        
        // 根据状态切换样式
        if stocksButton.isSelected {
            activated(button: stocksButton)
            disabled(button: favoritesButton)
        } else {
            activated(button: favoritesButton)
            disabled(button: stocksButton)
        }
    }
    
    // 禁用样式
    private func disabled(button: UIButton) {
        button.setTitleColor(.gray, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 18)
    }
    
    // 激活样式
    private func activated(button: UIButton) {
        button.setTitleColor(.black, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 28)
    }
}

关键细节

  • 通过addTarget绑定点击事件,确保每次按钮被点击时都会执行buttonTapped方法。
  • 利用isSelected状态标记当前激活的按钮,避免重复判断逻辑。
  • 样式方法独立封装,便于后续修改或扩展样式规则。

内容的提问来源于stack exchange,提问作者Said Rakhmiddin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 10:42:54