我正在使用滚动视图,但是遇到了我遇到的问题。我在Storyboard中创建了一个视图控制器。视图控制器包含一个滚动视图,该视图填充了整个超级视图。
然后我以编程方式将图像添加到滚动视图。图像确实显示在滚动视图中,并且分页效果很好。唯一的问题是将滚动视图设置为“填充”超级视图,但是保存图像的图像视图似乎停在导航栏所在的位置上方。如何使图像视图充满滚动视图中的整个视图?
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pagingView: UIPageControl!
var images = [UIImage]()
var frame = CGRect(x: 0, y: 0,width: 0,height: 0)
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.isPagingEnabled = true
images = [UIImage(named: "Slide1")!, UIImage(named: "Slide2")!, UIImage(named: "Slide3")!, UIImage(named: "Slide4")!]
pagingView.numberOfPages = images.count
// This is where I think I'm having the height problem.
for i in 0..<images.count {
let imageView = UIImageView()
let x = self.view.frame.size.width * CGFloat(i)
imageView.frame = CGRect(x: x, y: 0, width: self.view.frame.width, height: self.view.frame.height)
imageView.contentMode = .scaleAspectFill
imageView.image = images[i]
scrollView.contentSize.width = scrollView.frame.size.width * CGFloat(i + 1)
scrollView.addSubview(imageView)
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
pagingView.currentPage = Int(pageNumber)
}
在这种情况下,您需要启用父视图clipsToBounds。将
UIScrollview
clipsToBounds
属性设置为True。
以编程方式scrollView.clipsToBounds = true
在UIStoryBoard
中-单击视图->“属性”检查器