How do I change UIView Size?

2019-01-16 18:18发布

问题:

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?

回答1:

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)


回答2:

This can be achieved in various methods in Swift 3.0 Worked on Latest version Jan- 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)


回答3:

Swift 3 and Swift 4:

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


回答4:

Hi create this extends if you want. Update 2015 Swift 2.0

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

/**
Get Set x Position

- parameter x: CGFloat
by DaRk-_-D0G
*/
var x:CGFloat {
    get {
        return self.frame.origin.x
    }
    set {
        self.frame.origin.x = newValue
    }
}
/**
Get Set y Position

- parameter y: CGFloat
by DaRk-_-D0G
*/
var y:CGFloat {
    get {
        return self.frame.origin.y
    }
    set {
        self.frame.origin.y = newValue
    }
}
/**
Get Set Height

- parameter height: CGFloat
by DaRk-_-D0G
*/
var height:CGFloat {
    get {
        return self.frame.size.height
    }
    set {
        self.frame.size.height = newValue
    }
}
/**
Get Set Width

- parameter width: CGFloat
by DaRk-_-D0G
*/
var width:CGFloat {
    get {
        return self.frame.size.width
    }
    set {
        self.frame.size.width = newValue
    }
}

For Use (inherits Of UIView)

inheritsOfUIView.height = 100
button.height = 100
print(view.height)


回答5:

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...".