I have a background image @2x 7360x828px and @3x 11040x1242px used inside ScrollView, looks nice on all iPhones (full screen), but on iPad background image takes only half of the screen height.
I have tried many variations, with auto layout and without, with different contentMode, but I can't achieve my goal, either background image is very stretched, or image with correct aspect ratio is smaller than ScrollView.
My last code:
var imageView = UIImageView(image: UIImage(named: "map.png"))
lazy var scrollView = UIScrollView(frame: view.bounds)
override func viewDidLoad() {
super.viewDidLoad()
makeScrollView()
}
func makeScrollView() {
// For iPad
scrollView.backgroundColor = UIColor.black
scrollView.contentSize = imageView.bounds.size
scrollView.clipsToBounds = true
scrollView.contentMode = .scaleAspectFit
print("Scroll size: == ")
print(scrollView.contentSize)
scrollView.autoresizingMask = UIViewAutoresizing.flexibleWidth
scrollView.autoresizingMask = UIViewAutoresizing.flexibleHeight
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
scrollView.contentInsetAdjustmentBehavior = .never
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
imageView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(imageView)
imageView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
imageView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 7360 / 2).isActive = true
imageView.contentMode = .scaleAspectFit
}
How can I make full screen background image for iPad and keep aspect ratio?
The application is used only in landscape mode.
Screenshots:
1) contentMode = .scaleAspectFit
2) contentMode = .scaleAspectFill
3) .contentMode = .scaleToFill
5) The result I want to achieve. Full-screen image, with correct aspect ratio.