我正在尝试创建一个滚动浏览对象数组的全屏滚动视图。在故事板中,我只有一个滚动视图锚定在超视图的边缘,所以它的全屏。为阵列中的每个对象创建一个带有文本字段的新视图(参见代码)。
滚动视图在6s尺寸上完美运行,但是当它在SE模拟器上运行时,我会得到不同的结果。滚动视图完全搞砸了(参见下面)
以下是我的代码。其目标是为每个对象创建一个视图并显示其文本。
class ViewController: UIViewController {
//Create your connection from your storyboard
@IBOutlet weak var scrollView: UIScrollView!
// This is an array of views that will be appended to the scroll view later.
// These can be images or whatever want
var mainViews = [UIView]()
// This is an array of objects that are modeled off the SampleObjcet class we created earlier.
// They all contain diffrent strings to display. We will add the text as they are created
var arrayOfSampleObjects = [
SampleObject(sampleText: "Page 1"),SampleObject(sampleText: "Page 2"),
SampleObject(sampleText: "Page 3"),SampleObject(sampleText: "Page 4"),
]
override func viewDidLoad() {
super.viewDidLoad()
// contentWidth is a varible that controlls the width of the scroll view
// This variabel grows each time a view is added
var contentWidth: CGFloat = 0.0
// scrollviewWidth & scrollviewHeight hold the sizes so we can use them to
// calculate the diffrent views later
let scrollviewWidth = scrollView.frame.size.width
let scrollviewHeight = scrollView.frame.size.height
// This is where the magic happens.
// This for loop goes through the array of objects and create views for each object.
for object in 0..<arrayOfSampleObjects.count {
// textToShowInformation is a label made to show the text from the diffrent objects.
// Sets up text properties
let textToShowInformation = UILabel()
textToShowInformation.text = arrayOfSampleObjects[object].sampleText // Goes through the arrayOfSampleObjects infromation
textToShowInformation.translatesAutoresizingMaskIntoConstraints = false
textToShowInformation.textAlignment = .center
// viewToShowInformation is the view that wil be created for each object
// for example, if there are 50 objects, there will be 50 views.
let viewToShowInformation = UIView()
// This adds the diffrent views to the mainViews array to be displayed
mainViews.append(viewToShowInformation)
// ERROR
// come back to this.... the calculations only work for 6s -_-
var newX: CGFloat = 0.0
//object in an index so if its object 4,
//it will be 4x' s as wide...i think...
newX = scrollviewWidth * CGFloat(object)
contentWidth += newX - scrollviewWidth
scrollView.addSubview(viewToShowInformation)
viewToShowInformation.addSubview(textToShowInformation)
viewToShowInformation.frame = CGRect(x: newX, y:0, width: scrollviewWidth, height: scrollviewHeight)
// Constraints for the text fields. Center Everything...
textToShowInformation.centerXAnchor.constraint(equalTo: viewToShowInformation.centerXAnchor).isActive = true
textToShowInformation.centerYAnchor.constraint(equalTo: viewToShowInformation.centerYAnchor).isActive = true
}
// Sets up the scroll view properties
scrollView.bounces = true // allows views to bounce past the scrollview width. change to personal preferance
// Below controlls the width size of the scrollview,I think the problem could lie here too
scrollView.contentSize = CGSize(width: scrollView.frame.size.width * CGFloat(arrayOfSampleObjects.count), height:scrollviewHeight)
scrollView.clipsToBounds = false // important
}
}
一切都适用于6-6s,但当它在SE上运行时,文本不会居中。 文本被锚定到视图的中间,这是添加的
如果你在这里看不清楚,这里是回购的链接,你可以测试一下:object_controlled_scrollview
答案 0 :(得分:1)
您正在viewDidLoad
-
let scrollviewWidth = scrollView.frame.size.width
let scrollviewHeight = scrollView.frame.size.height
您的autoLayout尚未实施。因此,最初您的 scrollViewWidth 和 scrollViewHeight 将被设置为您在项目板中设计项目的任何大小。
尝试将设置UIScrollView
的整个代码块移动到viewDidLayoutSubviews
或viewDidAppear
或强>
您可以尝试在view.layoutIfNeeded()
super.viewDidLoad()
答案 1 :(得分:0)
尝试添加到以下代码行:
scrollView.setNeedsLayout()
scrollView.layoutIfNeeded()