求助:如何通过UITextField输入的CGFloat值修改UIView的Frame宽高?
Fixing UITextField Input to CGFloat for UIView Frame Adjustment
Hey there! I see you're having trouble converting user input from UITextFields into CGFloat values to adjust your UIView's size. Let's get that sorted out—here's how to modify your code to make it work, plus some best practices to handle edge cases.
Modified Code
First, here's the updated version of your class with all the fixes:
import UIKit class SetBorderSizeViewController: UIViewController{ @IBOutlet weak var widthTextField: UITextField! @IBOutlet weak var heightTextField: UITextField! @IBOutlet weak var borderView: UIView! override func viewDidLoad() { super.viewDidLoad() borderView.isHidden = true } @IBAction func changeSizeButtonTapped(_ sender: Any) { borderView.isHidden = false borderView.layer.borderWidth = 5 // Safely convert width input to CGFloat let widthInput = widthTextField.text ?? "" let targetWidth = CGFloat(Double(widthInput) ?? 80) // Fallback to 80 if input is invalid // Safely convert height input to CGFloat let heightInput = heightTextField.text ?? "" let targetHeight = CGFloat(Double(heightInput) ?? 20) // Fallback to 20 if input is invalid // Update the view's frame properly borderView.frame = CGRect( origin: borderView.frame.origin, size: CGSize(width: targetWidth, height: targetHeight) ) } }
Key Changes Explained
- Better Naming: I switched to Swift's standard camelCase for variable names (e.g.,
WidthText→widthTextField) to make the code more readable and maintainable. - Safe String-to-CGFloat Conversion:
- We first get the text from the text field, using
?? ""to handle cases where the text isnil. - We convert the string to a
Doublefirst (more reliable than directly converting toCGFloatacross different device architectures), then cast toCGFloat. - If the conversion fails (like if the user enters non-numeric text), we use your original hardcoded values as fallbacks.
- We first get the text from the text field, using
- Proper Frame Update: Instead of directly modifying
frame.size.height/width, we create a newCGRectthat preserves the view's original position (origin) while updating its size. This ensures the UI updates correctly every time.
Optional: Add Input Validation
If you want to notify users when they enter invalid values, you can add an alert for failed conversions. Here's how to adjust the button action:
@IBAction func changeSizeButtonTapped(_ sender: Any) { borderView.isHidden = false borderView.layer.borderWidth = 5 var targetWidth: CGFloat = 80 if let widthText = widthTextField.text, let widthDouble = Double(widthText) { targetWidth = CGFloat(widthDouble) } else { showInvalidInputAlert(for: "width") } var targetHeight: CGFloat = 20 if let heightText = heightTextField.text, let heightDouble = Double(heightText) { targetHeight = CGFloat(heightDouble) } else { showInvalidInputAlert(for: "height") } borderView.frame = CGRect(origin: borderView.frame.origin, size: CGSize(width: targetWidth, height: targetHeight)) } private func showInvalidInputAlert(for field: String) { let alert = UIAlertController( title: "Invalid Input", message: "Please enter a valid number for \(field).", preferredStyle: .alert ) alert.addAction(UIAlertAction(title: "OK", style: .default)) present(alert, animated: true) }
This will pop up a friendly alert whenever the user enters something that isn't a number, guiding them to fix their input.
内容的提问来源于stack exchange,提问作者Ammad
相关产品推荐
相关产品推荐

