如何实现可滚动动态按钮菜单?点击不切换控制器仅获标题
实现滚动视图中按钮的选中高亮效果
我明白你想要实现的是:点击滚动视图里的按钮时,只有当前选中的按钮显示蓝色标题,其他按钮恢复黑色,并且不需要切换ViewController,只在当前页面获取按钮标题。你的现有代码已经能创建按钮并响应点击,但缺少对之前选中按钮的状态重置,我们来调整一下:
步骤1:添加选中按钮的记录变量
首先在ViewController类里新增一个变量,用来追踪当前选中的按钮:
var selectedButton: UIButton?
步骤2:优化按钮点击事件处理
更新colorButtonPressed方法,先把之前选中的按钮恢复默认样式,再设置当前按钮为选中状态,同时获取按钮标题:
@objc func colorButtonPressed(sender:UIButton!){ // 获取并打印按钮标题,这里可以根据需求做后续处理 guard let buttonTitle = sender.title(for: .normal) else { return } print("当前选中的按钮标题:\(buttonTitle)") // 重置之前选中按钮的样式 selectedButton?.setTitleColor(.black, for: .normal) selectedButton?.layer.borderColor = UIColor.black.cgColor // 设置当前按钮的选中样式 sender.setTitleColor(.blue, for: .normal) sender.layer.borderColor = UIColor.blue.cgColor // 更新选中按钮的记录 selectedButton = sender }
步骤3:可选:设置默认选中的按钮
如果需要一开始就有一个默认选中的按钮,可以在viewDidLoad末尾添加这段代码:
// 默认选中第一个按钮 if let firstButton = scrollingView.subviews.first as? UIButton { colorButtonPressed(sender: firstButton) }
完整修改后的代码
class ViewController: UIViewController { @IBOutlet weak var productScrollView: UIScrollView! var buttonValues = ["Equity","Commodity","Derivatives","Market","Products","Values"] // 新增:记录当前选中的按钮 var selectedButton: UIButton? override func viewDidLoad() { super.viewDidLoad() let scrollingView = colorButtonsView(buttonSize: CGSize(width:100.0,height:30.0), buttonCount: buttonValues.count) productScrollView.contentSize = scrollingView.frame.size productScrollView.addSubview(scrollingView) productScrollView.showsHorizontalScrollIndicator = false productScrollView.indicatorStyle = .default productScrollView.setContentOffset(.zero, animated: false) // 设置默认选中第一个按钮(可选) if let firstButton = scrollingView.subviews.first as? UIButton { colorButtonPressed(sender: firstButton) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func colorButtonsView(buttonSize:CGSize, buttonCount:Int) -> UIView { let buttonView = UIView() buttonView.backgroundColor = UIColor.white buttonView.frame.origin = CGPoint(x:0,y:0) let padding = CGSize(width:10,height:10) buttonView.frame.size.width = (buttonSize.width + padding.width) * CGFloat(buttonCount) buttonView.frame.size.height = (buttonSize.height + 2.0 * padding.height) var buttonPosition = CGPoint(x:padding.width * 0.5,y: padding.height) let buttonIncrement = buttonSize.width + padding.width // 改用更规范的半开区间循环,避免潜在越界问题 for i in 0..<buttonCount { let button = UIButton(type: .custom) button.frame.size = buttonSize button.frame.origin = buttonPosition buttonPosition.x += buttonIncrement button.setTitle(buttonValues[i], for: .normal) button.setTitleColor(UIColor.black, for: .normal) button.layer.borderWidth = 1 button.layer.borderColor = UIColor.black.cgColor button.addTarget(self, action: #selector(colorButtonPressed(sender:)), for: .touchUpInside) buttonView.addSubview(button) } return buttonView } @objc func colorButtonPressed(sender:UIButton!){ guard let buttonTitle = sender.title(for: .normal) else { return } print("当前选中的按钮标题:\(buttonTitle)") // 重置之前选中按钮的样式 selectedButton?.setTitleColor(.black, for: .normal) selectedButton?.layer.borderColor = UIColor.black.cgColor // 设置当前按钮的选中样式 sender.setTitleColor(.blue, for: .normal) sender.layer.borderColor = UIColor.blue.cgColor selectedButton = sender } }
额外优化提示
如果想要更贴近你提供的动效(比如添加选中下划线、背景变色),可以在选中逻辑里添加图层操作,比如给选中按钮加下划线:
// 给当前选中按钮添加下划线 let underline = CALayer() underline.frame = CGRect(x: 0, y: sender.frame.height - 2, width: sender.frame.width, height: 2) underline.backgroundColor = UIColor.blue.cgColor underline.name = "underline" sender.layer.addSublayer(underline) // 移除之前选中按钮的下划线 selectedButton?.layer.sublayers?.filter { $0.name == "underline" }.forEach { $0.removeFromSuperlayer() }
这样修改后,就能实现你想要的点击按钮切换选中状态,且在当前ViewController获取标题的效果啦!
内容的提问来源于stack exchange,提问作者Ben10
相关产品推荐
相关产品推荐

