问题描述
我有一个视图,该视图内有一个滚动视图.我以编程方式设置了scrollview.但是由于某些原因,scrollview不能完美地适合视图.滚动视图与视图具有相同的框架.但是由于某种原因,它无法正常工作.
I have a view and inside the view there is a scrollview. I setup the scrollview programmatically. But for some reason the scrollview fits not perfectly in the view. The scrollview has the same frame as the view. But for some reason it is not working.
白色视图是滚动视图所在的视图.滚动视图是绿色视图.我将背景色设置为绿色.在滚动视图中有一个图像视图.
The white view is the view where the scrollview is in it.The scrollview is the green view. I set the background color to green.In the scrollview there is an image view.
我的代码:
var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.showsHorizontalScrollIndicator = false
scrollView.alwaysBounceVertical = false
scrollView.alwaysBounceHorizontal = false
scrollView.isPagingEnabled = true
return scrollView
}()
override func viewDidLoad() {
super.viewDidLoad()
scrollView.backgroundColor = .green
scrollView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: contentView.frame.height) contentView.addSubview(scrollView)
scrollView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
scrollView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
scrollView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
scrollView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
}
var frame = CGRect.zero
func viewTutorial() {
for i in 0..<arrayOfTutorilImages.count {
frame.origin.x = scrollView.frame.size.width * CGFloat((i))
frame.size = scrollView.frame.size
let imageView = UIImageView(frame: frame)
imageView.image = UIImage(named: arrayOfTutorilImages[i])
imageView.contentMode = .scaleAspectFit
self.scrollView.addSubview(imageView)
}
scrollView.contentSize = CGSize(width: (scrollView.frame.size.width * CGFloat(arrayOfTutorilImages.count)), height: scrollView.frame.size.height)
scrollView.delegate = self
}
extension TutorialViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
pageControl.currentPage = Int(pageNumber)
}
}
更新:我更改了框架并将约束添加到滚动视图.现在看起来像这样.图片没有调整大小(此图片是蓝色图片)
UPDATE:I changed the frame and added constraints to the scrollview. Now it look like this. The images is not resizing (this image is the blue drawing)
推荐答案
我建议您使用约束:
self.scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
self.scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
self.scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
如果您喜欢UIView + Helpers,则可以为此创建一个辅助方法,并且仅使用一行.
You can create a helper method for this if you have like a UIView+Helpers, and use just one line.
extension UIView {
public func pinToEdges(of view: UIView,
topConstant: CGFloat = 0,
leadingConstant: CGFloat = 0,
bottomConstant: CGFloat = 0,
trailingConstant: CGFloat = 0) {
NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo: view.topAnchor, constant: topConstant),
self.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: leadingConstant),
self.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: bottomConstant),
self.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: trailingConstant),
])
}
}
然后使用:
self.scrollView.pinToBounds(self.view)
当然要记住将 translatesAutoresizingMaskIntoConstraints
设置为false
And remember of course to set the translatesAutoresizingMaskIntoConstraints
to false
这篇关于如何将滚动视图的大小设置为与视图相同的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!