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

如何在Swift 5.1中不使用SwiftUI创建圆形计算器按钮

iOS UIKit 计算器按钮样式实现提问

大家好。
我目前正在学习iOS开发,首个尝试开发的应用是计算器APP。我已经开始搭建UI并创建了按钮,相关代码和应用运行截图如下。

private func setupNumPad(){
    let buttonSize: CGFloat = view.frame.size.width/4
    
    let zeroButton = UIButton(frame: CGRect(x: 0, y: holder.frame.size.height-buttonSize, width: buttonSize*3, height: buttonSize))
    zeroButton.setTitle("0", for: .normal)
    holder.addSubview(zeroButton)
    
    for x in 0..<3{
        let buttonOnetoThree = UIButton(frame: CGRect(x: buttonSize * CGFloat(x), y: holder.frame.size.height-(buttonSize*2), width: buttonSize, height: buttonSize))
        buttonOnetoThree.setTitle("\(x+1)", for: .normal)
        holder.addSubview(buttonOnetoThree)
    }
    
    for x in 0..<3{
        let buttonFourtoSix = UIButton(frame: CGRect(x: buttonSize * CGFloat(x), y: holder.frame.size.height-(buttonSize*3), width: buttonSize, height: buttonSize))
        buttonFourtoSix.setTitle("\(x+4)", for: .normal)
        holder.addSubview(buttonFourtoSix)
    }
    
    for x in 0..<3{
        let buttonSeventoNine = UIButton(frame: CGRect(x: buttonSize * CGFloat(x), y: holder.frame.size.height-(buttonSize*4), width: buttonSize, height: buttonSize))
        buttonSeventoNine.setTitle("\(x+7)", for: .normal)
        holder.addSubview(buttonSeventoNine)
    }
}

计算器应用运行效果图
我希望实现背景为白色的圆形按钮,若有人能讲解或展示具体实现方法我将非常感谢,欢迎提供任何帮助或指引。
我没有使用SwiftUI


解答

实现白色圆形按钮的核心是配置UIButton的layer属性,你可以抽离公共的样式配置方法避免重复写代码,具体实现步骤如下:

  1. 新增通用按钮样式配置方法,统一处理背景色、圆角、文字样式等属性:
private func configureButton(button: UIButton, buttonSize: CGFloat) {
    // 设置白色背景
    button.backgroundColor = .white
    // 配置文字样式,可根据需求调整字号和颜色
    button.setTitleColor(.black, for: .normal)
    button.titleLabel?.font = .systemFont(ofSize: 28, weight: .regular)
    // 正方形按钮圆角设为高度的1/2即可实现正圆效果,0号按钮高度与普通按钮一致,设置后为胶囊圆角,符合计算器常规设计
    button.layer.cornerRadius = buttonSize / 2
    // 可选配置:添加浅边框和阴影提升层次感
    button.layer.borderWidth = 1
    button.layer.borderColor = UIColor.systemGray5.cgColor
    button.layer.shadowColor = UIColor.black.cgColor
    button.layer.shadowOpacity = 0.08
    button.layer.shadowOffset = CGSize(width: 0, height: 2)
    button.layer.shadowRadius = 3
}
  1. 在setupNumPad方法中,每个按钮创建完成后调用上述配置方法即可,修改后的部分代码示例:
// 0号按钮配置
let zeroButton = UIButton(frame: CGRect(x: 0, y: holder.frame.size.height-buttonSize, width: buttonSize*3, height: buttonSize))
zeroButton.setTitle("0", for: .normal)
configureButton(button: zeroButton, buttonSize: buttonSize) // 新增样式配置
holder.addSubview(zeroButton)

// 1-3号按钮循环内新增配置
for x in 0..<3{
    let buttonOnetoThree = UIButton(frame: CGRect(x: buttonSize * CGFloat(x), y: holder.frame.size.height-(buttonSize*2), width: buttonSize, height: buttonSize))
    buttonOnetoThree.setTitle("\(x+1)", for: .normal)
    configureButton(button: buttonOnetoThree, buttonSize: buttonSize) // 新增样式配置
    holder.addSubview(buttonOnetoThree)
}
// 剩余4-6、7-9号按钮的循环内也加入同样的配置调用即可

如果需要按钮点击时的高亮反馈,可以额外给按钮添加对应状态的点击事件,调整高亮状态下的背景色即可。


内容的提问来源于stack exchange,提问作者Tanav Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 06:48:04