简体   繁体   English

iOS 方面配合和中心

[英]iOS aspect fit and center

I'd like to call我想打电话

[self.view setContentMode:UIViewContentModeScaleAspectFit | UIViewContentModeCenter];

where self is an instance of UIViewController .其中selfUIViewController的一个实例。 This doesn't seem to work;这似乎不起作用; what are the direct alternatives, if any?如果有的话,直接的替代品是什么?

The contentMode is not a mask; contentMode不是掩码; 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.如果内容不适合视图, UIViewContentModeScaleAspectFit应该将内容居中。

You will need to set contentMode once when the view loads and then center it in viewWillLayoutSubviews which is called upon device rotation.您将需要在视图加载时设置一次 contentMode,然后将其居中在设备旋转时调用的 viewWillLayoutSubviews 中。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM