我已經將UIImageView放在屏幕上的UIScrollView下,屏幕的寬度與屏幕的寬度相同,並且它在iPhone 5s中正常工作,但在iPhone 7或8上,圖像的大小小於寬度屏幕大小在屏幕截圖上共享。UIImageView在UIScrollview不適合頂部屏幕
這裏是我的代碼:
let x = CGFloat(selectedEvent.album?.count ?? 0)
pageControl.numberOfPages = (selectedEvent.album?.count ?? 0)
for index in 0..<(selectedEvent.album?.count ?? 0) {
frame.origin.x = scroll.frame.size.width * CGFloat(index)
frame.size = scroll.frame.size
print(self.frame.size.width)
print(scroll.frame.size.width)
print(frame.size.width)
let image = UIImageView(frame: frame)
image.contentMode = .scaleToFill
print(image.frame.size.width)
if (selectedEvent.album?.count ?? 0) > 0 {
selectedEvent.getAlbumImage(key: selectedEvent.album![index], completion: { (gotImage:UIImage) in
image.image = gotImage
})
} else {
image.image = UIImage(named: "defaultImage.jpg")
}
scroll .addSubview(image)
}
self.scroll.contentSize = CGSize(width: scroll.frame.size.width * x, height: scroll.frame.size.height)
pageControl.addTarget(self, action: #selector(self.changePage(sender:)), for: UIControlEvents.valueChanged)
這裏是打印日誌
320.0
320.0
320.0
320.0
320.0
320.0
320.0
320.0
不要用戶滾動視圖寬度,用[UIScreen mainScreen] .bounds.size.width;並在ViewWillAppear或ViewDidAppear中計算幀。 – Kuldeep
請檢查此:https://stackoverflow.com/questions/39021503/custom-view-self-frame-is-not-correct – Amit
我也遇到同樣的問題,並嘗試使用'layoutSubviews','setNeedsLayout'和' layoutIfNeeded'。沒有工作。然後我通過具有水平滾動的'collectionView'去除了滾動視圖。 – Amit