How do I change UIView Size?
Asked Answered
N

8

79

I have trouble with modifying my UIView height at launch.

I have to UIView and I want one to be screen size * 70 and the other to fill the gap.

here is what I have

 @IBOutlet weak var questionFrame: UIView!
 @IBOutlet weak var answerFrame: UIView!
 let screenSize:CGRect = UIScreen.mainScreen().bounds

and

 questionFrame.frame.size.height = screenSize.height * 0.70
 answerFrame.frame.size.height = screenSize.height * 0.30

It has no effect on the app during run time. I use autolayout but I only have margins constraints...

Am I doing it all wrong?

Naominaor answered 3/11, 2014 at 1:33 Comment(1)
questionFrame.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height * 0.7) answerFrame.frame = (0 , self.view.frame.height * 0.7, self.view.frame.width, self.view.frame.height * 0.3) Is somthing like this what you're after?Tref
T
66

Here you go. this should work.

questionFrame.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height * 0.7) 

answerFrame.frame =  CGRectMake(0 , self.view.frame.height * 0.7, self.view.frame.width, self.view.frame.height * 0.3)
Tref answered 3/11, 2014 at 3:5 Comment(3)
Just tried it on xcode 7.3. In order for this CGRectMake() to take effect, the UIView (my view is actually a subclassed from UIView) needs to have View->Mode set to "Redraw" or "Scale to Fit" and the CGRectMake() needs to be in my subclass' drawRect() method. Otherwise the size definitions in the storyboard will take effect.Counterglow
it's x y width height at the same time, it's better to use userView.frame.size.height = 10 userView.frame.size.width = 10Poussette
CGRectMake was renamed to CGRect. The following code should work on newer Swift versions: questionFrame.frame = CGRect(x: 0 , y: 0, width: self.view.frame.width, height: self.view.frame.height * 0.7) answerFrame.frame = CGRect(x: 0 , y: 0, width: self.view.frame.height * 0.7, self.view.frame.width, height: self.view.frame.height * 0.3)Lionel
M
74

This can be achieved in various methods in Swift 3.0 Worked on Latest version MAY- 2019

Directly assign the Height & Width values for a view:

userView.frame.size.height = 0

userView.frame.size.width = 10

Assign the CGRect for the Frame

userView.frame =  CGRect(x:0, y: 0, width:0, height:0)

Method Details:

CGRect(x: point of X, y: point of Y, width: Width of View, height: Height of View)

Using an Extension method for CGRECT

Add following extension code in any swift file,

extension CGRect {

    init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {

        self.init(x:x, y:y, width:w, height:h)
    }
}

Use the following code anywhere in your application for the view to set the size parameters

userView.frame =  CGRect(1, 1, 20, 45)
Masefield answered 20/4, 2017 at 20:17 Comment(0)
T
66

Here you go. this should work.

questionFrame.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height * 0.7) 

answerFrame.frame =  CGRectMake(0 , self.view.frame.height * 0.7, self.view.frame.width, self.view.frame.height * 0.3)
Tref answered 3/11, 2014 at 3:5 Comment(3)
Just tried it on xcode 7.3. In order for this CGRectMake() to take effect, the UIView (my view is actually a subclassed from UIView) needs to have View->Mode set to "Redraw" or "Scale to Fit" and the CGRectMake() needs to be in my subclass' drawRect() method. Otherwise the size definitions in the storyboard will take effect.Counterglow
it's x y width height at the same time, it's better to use userView.frame.size.height = 10 userView.frame.size.width = 10Poussette
CGRectMake was renamed to CGRect. The following code should work on newer Swift versions: questionFrame.frame = CGRect(x: 0 , y: 0, width: self.view.frame.width, height: self.view.frame.height * 0.7) answerFrame.frame = CGRect(x: 0 , y: 0, width: self.view.frame.height * 0.7, self.view.frame.width, height: self.view.frame.height * 0.3)Lionel
D
33

Swift 3 and Swift 4:

myView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
Dance answered 15/1, 2017 at 9:3 Comment(0)
S
20

Hi create this extends if you want. Update 2021 Swift 5

Create File Extends.Swift and add this code (add import foundation where you want change height)

extension UIView {

    var x: CGFloat {
        get {
            self.frame.origin.x
        }
        set {
            self.frame.origin.x = newValue
        }
    }

    var y: CGFloat {
        get {
            self.frame.origin.y
        }
        set {
            self.frame.origin.y = newValue
        }
    }

    var height: CGFloat {
        get {
            self.frame.size.height
        }
        set {
            self.frame.size.height = newValue
        }
    }

    var width: CGFloat {
        get {
            self.frame.size.width
        }
        set {
            self.frame.size.width = newValue
        }
    }
}

For Use (inherits Of UIView)

inheritsOfUIView.height = 100
button.height = 100
print(view.height)
Slew answered 27/2, 2015 at 13:45 Comment(1)
It's not possible to set the width and so on without a Initialization is only a get property !Berke
C
2

For a progress bar kind of thing, in Swift 4

I follow these steps:

  1. I create a UIView outlet : @IBOutlet var progressBar: UIView!
  2. Then a property to increase its width value after a user action var progressBarWidth: Int = your value
  3. Then for the increase/decrease of the progress progressBar.frame.size.width = CGFloat(progressBarWidth)
  4. And finally in the IBACtion method I add progressBarWidth += your value for auto increase the width every time user touches a button.
Centi answered 19/2, 2019 at 11:9 Comment(0)
V
1

You can do this in Interface Builder:

  1. Control-drag from a frame view (e.g. questionFrame) to main View, in the pop-up select Equal heights.

  2. Then go to size inspector of the frame, click edit Equal height to Superview constraint, set the multiplier to 0.7 and hit return.

You'll see that constraint has changed from Equal height to... to Proportional height to....

Verduzco answered 1/11, 2017 at 10:52 Comment(0)
P
1

Assigning questionFrame.frame.size.height= screenSize.height * 0.30 will not reflect anything in the view. because it is a get-only property. If you want to change the frame of questionFrame you can use the below code.

questionFrame.frame = CGRect(x: 0, y: 0, width: 0, height: screenSize.height * 0.70)
Paleoecology answered 19/2, 2019 at 11:27 Comment(0)
H
0

I know that there is already a solution to this question, but I found an alternative to this issue and maybe it could help someone.

I was having trouble with setting the frame of my sub view because certain values were referring to its position within the main view. So if you don't want to update your frame by changing the whole frame via CGRect, you can simply change a value of the frame and then update it.

// keep reference to you frames
var questionView = questionFrame.frame
var answerView = answerFrame.frame

// update the values of the copy        
questionView.size.height = CGFloat(screenSize.height * 0.70)
answerView.size.height = CGFloat(screenSize.height * 0.30)

// set the frames to the new frames
questionFrame.frame = questionView
answerFrame.frame = answerView
Homoousian answered 28/6, 2019 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.