Swift - 在横向时创建 UIButton
Posted
技术标签:
【中文标题】Swift - 在横向时创建 UIButton【英文标题】:Swift - Creating UIButton when in landscape 【发布时间】:2016-02-12 07:20:57 【问题描述】:我正在 ios 9、Swift 和 Xcode 7 中制作计算器应用程序,我想知道当用户仅切换到横向模式时如何创建 UIButtons。在数字和AC Button
的左侧添加更多运算符并更改constraints
,将按钮移到上方,为新的按钮腾出空间。 iOS的基本计算器可以做到这一点,我想知道如何?
我是 iOS 开发的新手,我一直在浏览网页,只发现一个名为:viewWillTransitionToSize()
的覆盖函数。这是做这种事情的正确功能吗?
此外,每次我进入横向视图时,都会创建另一个按钮。我怎样才能阻止这种情况发生?
附件是我以编程方式创建的 UIbutton 和上面列出的功能。
对于一系列问题/问题,我们深表歉意。我很难找到有关此问题的教程或帮助。任何指导或解决方案都会有所帮助。非常感谢。
ViewController.swift
import UIKit
class ViewController: UIViewController
@IBOutlet weak var outputDisplay: UILabel!
@IBOutlet weak var number7: UIButton!
@IBOutlet weak var number1: UIButton!
@IBOutlet weak var number2: UIButton!
@IBOutlet weak var number3: UIButton!
@IBOutlet weak var number4: UIButton!
@IBOutlet weak var number5: UIButton!
@IBOutlet weak var number6: UIButton!
@IBOutlet weak var number9: UIButton!
@IBOutlet weak var number8: UIButton!
@IBOutlet weak var numberZero: UIButton!
@IBOutlet weak var decimalPoint: UIButton!
@IBOutlet weak var plusMinusToggle: UIButton!
@IBOutlet weak var acClear: UIButton!
@IBOutlet weak var equalButton: UIButton!
@IBOutlet weak var multiply: UIButton!
@IBOutlet weak var divide: UIButton!
@IBOutlet weak var subtraction: UIButton!
@IBOutlet weak var addition: UIButton!
@IBOutlet weak var deletion: UIButton!
@IBOutlet weak var squareRoot: UIButton!
var typing:Bool = false
var firstNumber: Float = 0.0
var secondNumber: Float = 0.0
var operation:String = ""
var result: Float = 0.0
var valueToPass: Float = 0.0
override func viewDidLoad()
super.viewDidLoad()
number1.layer.cornerRadius = number1.bounds.size.width / 8.0
number2.layer.cornerRadius = number2.bounds.size.width / 8.0
number3.layer.cornerRadius = number3.bounds.size.width / 8.0
number4.layer.cornerRadius = number4.bounds.size.width / 8.0
number5.layer.cornerRadius = number5.bounds.size.width / 8.0
number6.layer.cornerRadius = number6.bounds.size.width / 8.0
number7.layer.cornerRadius = number7.bounds.size.width / 8.0
number8.layer.cornerRadius = number8.bounds.size.width / 8.0
number9.layer.cornerRadius = number9.bounds.size.width / 8.0
numberZero.layer.cornerRadius = numberZero.bounds.size.width / 8.0
equalButton.layer.cornerRadius = equalButton.bounds.size.width / 8.0
deletion.layer.cornerRadius = deletion.bounds.size.width / 8.0
plusMinusToggle.layer.cornerRadius = plusMinusToggle.bounds.size.width / 8.0
decimalPoint.layer.cornerRadius = decimalPoint.bounds.size.width / 8.0
acClear.layer.cornerRadius = acClear.bounds.size.width / 8.0
addition.layer.cornerRadius = addition.bounds.size.width / 8.0
divide.layer.cornerRadius = divide.bounds.size.width / 8.0
multiply.layer.cornerRadius = multiply.bounds.size.width / 8.0
subtraction.layer.cornerRadius = subtraction.bounds.size.width / 8.0
squareRoot.layer.cornerRadius = squareRoot.bounds.size.width / 8.0
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
if (UIDevice.currentDevice().orientation.isLandscape)
print("Device in Lanscape")
let newButton1 = UIButton(type: UIButtonType.System) as UIButton
newButton1.frame = CGRectMake(100, 100, 100, 50)
newButton1.backgroundColor = UIColor.whiteColor()
newButton1.center = CGPoint(x: number1.frame.width, y: number1.frame.height)
newButton1.layer.cornerRadius = newButton1.bounds.size.width / 8.0
self.view.addSubview(newButton1)
else
@IBAction func numberPressed(sender: AnyObject)
// set the variable "number" to whichever number the user presses
let number = sender.currentTitle!
if typing
outputDisplay.text = outputDisplay.text! + String(number!)
else
outputDisplay.text = number!
typing = true
@IBAction func calculationPressed(sender: AnyObject)
typing = false
firstNumber = Float(outputDisplay.text!)!
operation = sender.currentTitle!!
@IBAction func equalPressed(sender: AnyObject)
secondNumber = Float(outputDisplay.text!)!
typing = false
//arithmetic operations
if operation == "+"
result = firstNumber + secondNumber
else if operation == "-"
result = firstNumber - secondNumber
else if operation == "✕"
result = firstNumber * secondNumber
else if operation == "÷"
result = firstNumber / secondNumber
// else if operation == "%"
// result = (firstNumber * secondNumber) / 100
//
else if operation == "√"
result = sqrt(firstNumber)
outputDisplay.text = "\(result)"
@IBAction func squareRoot(sender: AnyObject)
result = sqrt(Float(outputDisplay.text!)!)
outputDisplay.text = "\(result)"
@IBAction func deletePressed(sender: AnyObject)
if outputDisplay.text!.characters.count != 0
outputDisplay.text = outputDisplay.text!.substringToIndex(outputDisplay.text!.endIndex.predecessor())
else
outputDisplay.text = "0"
@IBAction func plusMinus(sender: AnyObject)
if( outputDisplay.text![outputDisplay.text!.startIndex] == "-")
outputDisplay.text!.removeAtIndex(outputDisplay.text!.startIndex)
else
outputDisplay.text!.insert("-", atIndex: outputDisplay.text!.startIndex)
@IBAction func decimal(sender: AnyObject)
let decimal = sender.currentTitle!
outputDisplay.text = outputDisplay.text! + decimal!
@IBAction func clear(sender: AnyObject)
result = 0
firstNumber = 0
secondNumber = 0
outputDisplay.text = "0"
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【问题讨论】:
与您的问题无关,但是:您为什么不使用 switch case 而不是 if else if 语句?我认为 switch case 会更合适,因为 if 语句必须测试每个表达式。 【参考方案1】:我会使用Size Classes 来执行此操作。您可以使用不同的尺寸类别在情节提要中设计不同的布局和按钮排列。如果此应用程序在 iPhone 上,您将在纵向时使用紧凑宽度和常规高度,在横向时使用紧凑高度和常规宽度。
【讨论】:
以上是关于Swift - 在横向时创建 UIButton的主要内容,如果未能解决你的问题,请参考以下文章
swift - 当设备旋转到横向(iPhone/iPad)时,如何让 UIView 变成全屏?