skip to Main Content

I read the documentation, but I just can’t figure out what exactly is needed in my simple case.
The element is constantly jumping. I need just that.
I would be grateful for articles and so on. For a deeper understanding of this

enter image description here

class MyViewController : UIViewController {
    override func loadView() {
    
        let view = UIView()
        view.backgroundColor = .white
        
        let imageView = UIImageView()
        
        view.addSubview(imageView)
       
        
        self.view = view
    }
}

2

Answers


  1. You are looking for auto layout constraints.
    E.g. add to your code:

    imageView.translatesAutoresizingMaskIntoConstraints = false
    
    // add desired constraints here.
    imageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    ...
    

    You may want to set top, leading and trailing anchors, depending on the desired behavior.

    Login or Signup to reply.
  2. imageView.translatesAutoresizingMaskIntoConstraints = false
    
    // Apply constraint
    
    NSLayoutConstraint.activate([
        imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
        imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
        imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
        imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1)
    ])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search