-1

I am able to change height and width of UIView frame on button click by hardcoding the height and width, but i am unable change it when i get input from user in UItext field in CGFloat format.

`

 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
   }

   }

`

Anyone here who can update my code according to my requirements please???.

Ammad
  • 3
  • 3

2 Answers2

0

Try this:

@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()
       }
Shabbir Ahmad
  • 615
  • 7
  • 17
  • Cannot invoke initializer for type 'Int' with an argument list of type '(String?)' @Shabbir Ahmad – Ammad May 18 '18 at 12:15
0

This code works as well:

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))
    }

}

but in my case I am using constraints for my "resizableView" so my view invalidated each time. In this case you can create reference for width and height constraints and set them new values like:

widthConstraint.constant = <your_value>
eugene
  • 86
  • 2