如何在 Swift 5.1 中创建一个圆形计算器按钮?
How to create a circular calculator button in Swift 5.1?
希望你们一切都好。
我正在学习 iOS 开发,正如我们所说的。我尝试构建的第一个应用程序是计算器应用程序。我已经开始构建 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)
}
Click Here to View Image of Code Running
但是,如果有人能解释或告诉我如何创建一个圆形按钮,我会很高兴。让我们说我想要的圆形按钮的背景是白色的。任何帮助或指导将不胜感激。
** 我没有使用 SwiftUI **
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))
// make circle button
buttonOnetoThree.layer.cornerRadius = buttonSize/2
buttonOnetoThree.backgroundColor = .white
buttonOnetoThree.setTitle("\(x+1)", for: .normal)
holder.addSubview(buttonOnetoThree)
}
欢迎来到社区。为了创建一个圆形按钮,您必须更改按钮的 layer.corderRadius
属性(提示 - 任何 UIView
也是如此)。要使其成为完美的圆,您必须将 cornerRadius
设置为您为宽度或高度设置的值的一半 (0.5)。以您的 for-loop
之一为例,您将必须进行以下更改,
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)
// SETTING THE CORNER RADIUS TO MAKE A CIRCULAR BUTTON -----------------
buttonOnetoThree.layer.cornerRadius = 0.5 * buttonSize // Setting corner radius to half of buttonSize
buttonOnetoThree.clipsToBounds = true // This value makes sure that the view (Button) is within the frame defined
holder.addSubview(buttonOnetoThree)
}
希望你们一切都好。
我正在学习 iOS 开发,正如我们所说的。我尝试构建的第一个应用程序是计算器应用程序。我已经开始构建 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)
}
Click Here to View Image of Code Running
但是,如果有人能解释或告诉我如何创建一个圆形按钮,我会很高兴。让我们说我想要的圆形按钮的背景是白色的。任何帮助或指导将不胜感激。
** 我没有使用 SwiftUI **
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))
// make circle button
buttonOnetoThree.layer.cornerRadius = buttonSize/2
buttonOnetoThree.backgroundColor = .white
buttonOnetoThree.setTitle("\(x+1)", for: .normal)
holder.addSubview(buttonOnetoThree)
}
欢迎来到社区。为了创建一个圆形按钮,您必须更改按钮的 layer.corderRadius
属性(提示 - 任何 UIView
也是如此)。要使其成为完美的圆,您必须将 cornerRadius
设置为您为宽度或高度设置的值的一半 (0.5)。以您的 for-loop
之一为例,您将必须进行以下更改,
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)
// SETTING THE CORNER RADIUS TO MAKE A CIRCULAR BUTTON -----------------
buttonOnetoThree.layer.cornerRadius = 0.5 * buttonSize // Setting corner radius to half of buttonSize
buttonOnetoThree.clipsToBounds = true // This value makes sure that the view (Button) is within the frame defined
holder.addSubview(buttonOnetoThree)
}