Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change the height and width of a UIImageView Xcode Swift

Hey for some reason I am struggling with trying to set the height and width of one of my image views. I want to set it so the height only goes for 20% of my screen. I know to regularly set it you can do things like: image = (0,0,50,50)

But I need the height to not be a static number. Something like image = (0,0, frame.height * 0.2, 50)

Any suggestions?

like image 455
Lucas Azzopardi Avatar asked Jul 10 '15 16:07

Lucas Azzopardi


People also ask

How do I change the size of my UIImageView?

The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options. I would NOT retain the result here. newImage will be autoreleased already, and it should up to the method that called this to retain it or not.

How do I change the height of a TextField in swift?

Step 1 : Click the Attribute Inspector, Select the Border Styles which is not the rounded one. Step 2 : Now go to Size Inspector and change the size of the TextField. Step 3 : Create an @IBOutlet for TextField and then add below code inside the viewWillAppear() or viewDidAppear() .

How do I change aspect ratio in UIImageView?

To maintain aspect ratio I calculated the width of UIImageView = (320/460)*450 = 313.043 dynamically. And set the contentMode For UIImageView is UIViewContentModeScaleAspectFit. And set the image(320x460) to image view but it is some what blur.


1 Answers

The accepted answer in Swift 3:

let screenSize: CGRect = UIScreen.main.bounds image.frame = CGRect(x: 0, y: 0, width: 50, height: screenSize.height * 0.2) 
like image 130
user1333394 Avatar answered Oct 05 '22 23:10

user1333394