Tuesday, November 11, 2025
HomeiOS DevelopmentUnderstanding format anchors in Swift

Understanding format anchors in Swift

[ad_1]

So, I’ve this ViewController the place I render a picture inside a subview.

class ViewController: UIViewController {
  override func viewDidLoad() {
    tremendous.viewDidLoad()
    view.backgroundColor = .systemBackground
    
    let childView = UIView()
    childView.backgroundColor = .purple
    
    let imageView = UIImageView(picture: UIImage(systemName: "tray"))
    imageView.contentMode = .scaleAspectFill
    
    childView.addSubview(imageView)
    
    imageView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      imageView.heightAnchor.constraint(equalToConstant: 100.0)
    ])
    
    view.addSubview(childView)
    
    childView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      childView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor)
    ])
  }
}

As you possibly can see for some cause, the picture strikes to the left of display screen. What’s the explanation for that?

enter image description here

Yet one more factor I seen is the subview ought to have a background shade of purple as specified, however in some way it is clear. Why is that?

I count on the end result to be one thing like,

enter image description here

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments