IOS - How to scale a view by Swift?

Asked

Viewed 166 times

4

I have a view called viewV and would like to change your Width and Height shown in the image below via Swift code.

inserir a descrição da imagem aqui

  • You can use the option Aspect Ratio

2 answers

6


You can do this with this code below :

let x = CGFloat(0)
let y = CGFloat(0)
let height = CGFloat(100)
let width = CGFloat(100)

viewV.frame = CGRectMake(x, y, height, width)

Your normal viewV

inserir a descrição da imagem aqui

Your viewV Modified with the code inserir a descrição da imagem aqui

1

It would be interesting for you to use NSLayoutConstraints for this. Would be via code (more or less) like this:

// Constraints
let topConstraint = NSLayoutConstraint(item: viewV, attribute: .top, relatedBy: .equal, toItem: viewV.superview!, attribute: .top, multiplier: 1, constant: 0)
let leadConstraint = NSLayoutConstraint(item: viewV, attribute: .leading, relatedBy: .equal, toItem: viewV.superview!, attribute: .leading, multiplier: 1, constant: 0)
let widthConstraint = NSLayoutConstraint(item: viewV, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 667)
let heightConstraint = NSLayoutConstraint(item: viewV, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 267)
NSLayoutConstraint.activate([topConstraint, leadConstraint, widthConstraint, heightConstraint])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.