根据文本字段的用户输入更改 UIView 框架的高度和宽度
Change UIView frame Height and width from user input from UIText field
我可以通过对高度和宽度进行硬编码来更改单击按钮时 UIView 框架的高度和宽度,但是当我在 UItext 字段中以 CGFloat 格式从用户那里获得输入时,我无法更改它。
`
import UIKit
class SetBorderSizeViewController: UIViewController{
@IBOutlet weak var WidthText: UITextField!
@IBOutlet weak var Height: UITextField!
@IBOutlet weak var View_border: UIView!
override func viewDidLoad() {
super.viewDidLoad()
View_border.isHidden = true
}
//Change UIView Height and width frame on "ChangeSizeButton" Button Press.....
@IBAction func ChangeSizeButton(_ sender: Any) {
View_border.isHidden = false
View_border.layer.borderWidth = 5
/// Here i want to get user input from text field...
View_border.frame.size.height = 20
View_border.frame.size.width = 80
}
}
`
这里有人可以根据我的要求更新我的代码吗???。
试试这个:
@IBAction func ChangeSizeButton(_ sender: Any) {
View_border.isHidden = false
View_border.layer.borderWidth = 5
/// Here i want to get user input from text field...
View_border.frame.size.height = CGFloat(Float(WidthText.text!)!)
View_border.frame.size.width = CGFloat(Float(Height.text!)!)
self.View_border.layoutIfNeeded()
}
此代码也适用:
class ViewController: UIViewController {
// MARK: IBOutlets
@IBOutlet private weak var resizableView: UIView!
@IBOutlet private weak var widthTextField: UITextField!
@IBOutlet private weak var heightTextField: UITextField!
// MARK: IBActions
@IBAction private func didResizeButtonTapped(_ sender: UIButton) {
let (newWidth, newHeight) = convertValues()
resizableView.frame.size.width = newWidth
resizableView.frame.size.height = newHeight
}
}
// MARK: Private Methods
private extension ViewController {
func convertValues() -> (width: CGFloat, height: CGFloat) {
let width = Float(widthTextField.text ?? "0")
let height = Float(heightTextField.text ?? "0")
return (CGFloat(width ?? 0.0), CGFloat(height ?? 0.0))
}
}
但在我的例子中,我对我的 "resizableView" 使用了约束,所以我的视图每次都无效。在这种情况下,您可以为宽度和高度约束创建参考并将它们设置为新值,例如:
widthConstraint.constant = <your_value>
我可以通过对高度和宽度进行硬编码来更改单击按钮时 UIView 框架的高度和宽度,但是当我在 UItext 字段中以 CGFloat 格式从用户那里获得输入时,我无法更改它。
`
import UIKit
class SetBorderSizeViewController: UIViewController{
@IBOutlet weak var WidthText: UITextField!
@IBOutlet weak var Height: UITextField!
@IBOutlet weak var View_border: UIView!
override func viewDidLoad() {
super.viewDidLoad()
View_border.isHidden = true
}
//Change UIView Height and width frame on "ChangeSizeButton" Button Press.....
@IBAction func ChangeSizeButton(_ sender: Any) {
View_border.isHidden = false
View_border.layer.borderWidth = 5
/// Here i want to get user input from text field...
View_border.frame.size.height = 20
View_border.frame.size.width = 80
}
}
`
这里有人可以根据我的要求更新我的代码吗???。
试试这个:
@IBAction func ChangeSizeButton(_ sender: Any) {
View_border.isHidden = false
View_border.layer.borderWidth = 5
/// Here i want to get user input from text field...
View_border.frame.size.height = CGFloat(Float(WidthText.text!)!)
View_border.frame.size.width = CGFloat(Float(Height.text!)!)
self.View_border.layoutIfNeeded()
}
此代码也适用:
class ViewController: UIViewController {
// MARK: IBOutlets
@IBOutlet private weak var resizableView: UIView!
@IBOutlet private weak var widthTextField: UITextField!
@IBOutlet private weak var heightTextField: UITextField!
// MARK: IBActions
@IBAction private func didResizeButtonTapped(_ sender: UIButton) {
let (newWidth, newHeight) = convertValues()
resizableView.frame.size.width = newWidth
resizableView.frame.size.height = newHeight
}
}
// MARK: Private Methods
private extension ViewController {
func convertValues() -> (width: CGFloat, height: CGFloat) {
let width = Float(widthTextField.text ?? "0")
let height = Float(heightTextField.text ?? "0")
return (CGFloat(width ?? 0.0), CGFloat(height ?? 0.0))
}
}
但在我的例子中,我对我的 "resizableView" 使用了约束,所以我的视图每次都无效。在这种情况下,您可以为宽度和高度约束创建参考并将它们设置为新值,例如:
widthConstraint.constant = <your_value>