6

I'd like to call

[self.view setContentMode:UIViewContentModeScaleAspectFit | UIViewContentModeCenter];

where self is an instance of UIViewController. This doesn't seem to work; what are the direct alternatives, if any?

C0D3
  • 6,440
  • 8
  • 43
  • 67
Ken
  • 30,811
  • 34
  • 116
  • 155

2 Answers2

11

The contentMode is not a mask; you can't use | to combine values, you'll have to pick one. UIViewContentModeScaleAspectFit should center the content if it doesn't fit the view.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
2

You will need to set contentMode once when the view loads and then center it in viewWillLayoutSubviews which is called upon device rotation.

override func viewDidLoad() {
  super.viewDidLoad()
  //additional code to instantiate and setup image
  imageView.contentMode = .ScaleAspectFit
  self.view.addSubview(profileImageView)
}

override func viewWillLayoutSubviews() {
    imageView.center.x = view.center.x
    //or imageView.center = view.center
    //or imageView.center.y = view.center.y
}
ericgu
  • 2,229
  • 23
  • 25