I have one UIViewController in my Swift 3.3 UIViewController , in which I need to set a non-standard header. Before iOS11, I did it this way (I’m not presenting all the code, but only the key points):

 class MyController : UIViewController { private lazy var customTitleView : UIImageView = { $0.frame = CGRect(x:0, y:0, width:10, height:10) $0.contentMode = .scaleAspectFit $0.clipsToBounds = true return $0 }(UIImageView(image:UIImage(named:"my_image"))) override func viewDidLoad() { super.viewDidLoad() self.navigationItem.titleView = self.customTitleView } } 

Previously, thanks to this code, the self.navigationItem header was always centered, and the image kept the size I specified in the initializer. Now in iOS11, this header is stretched across the entire height, the dimensions I specified are ignored. How can I get back the old behavior of the program?

    1 answer 1

    In the end, I decided to do the following: wrap the image-logo, which I showed in titleView , into an object of the UIView successor UIView , and in this object, adjust the size of the image as a child. The result was something like the following:

     class LogoView : UIView { private static let rootImageTopOffset : CGFloat = 10.0 // отступ сверху для изображения private static let rootImageBottomOffset : CGFloat = 5.0 // отступ снизу для изображения //Изображение, ради которого всё и затевалось private lazy var rootImageView : UIImageView = { $0.contentMode = .scaleAspectFit $0.clipsToBounds = true return $0 }(UIImageView(image:UIImage(named:"my_image"))) override init(frame: CGRect) { super.init(frame: frame) setupSubviews() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setupSubviews() } private func setupSubviews() { self.addSubview(self.rootImageView) } //Функцию setupFrame я вызываю, когда мне нужно поменять размеры картинки (например, после поворота экрана) final func setupFrame(rootFrame:CGRect) { self.frame = rootFrame self.rootImageView.frame = CGRect(x: 0, y: LogoView.rootImageTopOffset, width: rootFrame.size.width, height: rootFrame.size.height - LogoView.rootImageTopOffset - LogoView.rootImageBottomOffset) } } 

    After that, I use the object of the LogoView class as the titleView of my UINavigationItem . In no case do I claim that this solution is the best, but I couldn’t think of anything better.