简体   繁体   English

Scrollview ContentSize

[英]Scrollview ContentSize

I am trying to make a scrollview with 3 pictures on a sign up/join page for a small app I trying to make. 我正在尝试为一个我要制作的小应用程序在注册/加入页面上制作3张图片的滚动视图。 I was using this code below: 我在下面使用此代码:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    var signInButton: UIButton!
    var joinButton: UIButton!
    var pageControl: UIPageControl!
    @IBOutlet weak var scrollView: UIScrollView!
    var colors:[UIColor] = [UIColor.redColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.yellowColor()]
    var frame = CGRectMake(0, 0, 0, 0)

    override func viewDidLoad() {
        super.viewDidLoad()

        signInButton = UIButton(frame: CGRectMake(1/3.0 * self.view.bounds.size.width, 5/6.0 * self.view.bounds.size.height, 23, 60))

        joinButton = UIButton(frame: CGRectMake(2/3.0 * self.view.bounds.size.width, 5/6.0 * self.view.bounds.size.height, 23, 60))

        pageControl = UIPageControl(frame: CGRectMake(1/2.0 * self.view.bounds.size.width, 70/100.0 * self.view.bounds.size.height, 23, 60))

        self.view.addSubview(signInButton)
        self.view.addSubview(joinButton)
        self.view.addSubview(pageControl)

        signInButton.addTarget(self, action: "signInButtonClicked:", forControlEvents: .TouchUpInside)
        joinButton.addTarget(self, action: "joinButtonClicked:", forControlEvents: .TouchUpInside)
        pageControl.addTarget(self, action: Selector("changePage:"), forControlEvents: UIControlEvents.ValueChanged)

        configurePageControl()
        scrollView.delegate = self

        for index in 0...3 {
            frame.size = scrollView.frame.size
            frame.origin.x = scrollView.frame.size.width * CGFloat(index)
            scrollView.pagingEnabled = true

            let view: UIView = UIView(frame: frame)
            view.backgroundColor = colors[index]
            scrollView.addSubview(view)
        }

        self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 4, self.scrollView.frame.size.height)
    }

    func configurePageControl() {
        self.pageControl.numberOfPages = colors.count
        self.pageControl.currentPage = 0
        self.pageControl.tintColor = UIColor.redColor()
        self.pageControl.pageIndicatorTintColor = UIColor.blackColor()
        self.pageControl.currentPageIndicatorTintColor = UIColor.greenColor()
    }

I put a scrollview on my view controller (using the size class width: Any and height: Any ) and pinned all four sides of the scrollview to the view controller. 我在我的视图控制器上放置了一个滚动视图(使用size类width: Anyheight: Any ),并将scrollview的所有四个侧面固定在视图控制器上。 For some reason each of the views I add to the scrollview are not exactly the same width as the iPhone. 由于某种原因,我添加到滚动视图中的每个视图的宽度与iPhone的宽度并不完全相同。

For example when, I tried running it on an iPhone 6 the first view extends past the width of the iPhone. 例如,当我尝试在iPhone 6上运行它时,第一个视图超出了iPhone的宽度。 And that happens to the second and third view. 第二和第三种观点就是这样。 I want the first view to be the exact width of the iPhone and the second view to be exactly to the right of the first view and so on. 我希望第一个视图是iPhone的确切宽度,第二个视图恰好在第一个视图的右边,依此类推。 There seems to be some overlapping with the first view going past the width of the iPhone. 超出iPhone宽度的第一个视图似乎有些重叠。

Could this be because I am using the size class (width: any and height: any) and I should disable size classes and add a scrollview for each iPhone width? 难道是因为我正在使用尺寸类别(宽度:任意和高度:任意),并且应该禁用尺寸类别并为每个iPhone宽度添加滚动视图?

Can someone help me identify the problem here. 有人可以帮我在这里找到问题。

Add: 加:

scrollView.setNeedsLayout()
scrollView.layoutIfNeeded()

at the beginning of the viewDidLoad() method. viewDidLoad()方法的开头。

Always call these two methods before accessing a view's frame when using Auto Layout, otherwise you'll probably get an incorrect size. 使用“自动版式”访问视图的框架之前,请始终调用这两种方法,否则可能会得到不正确的尺寸。

The problem is that when viewDidLoad() is called, your view still has a width of 600 (due to your storyboard view controller size of 600x600 ). 问题在于,当调用viewDidLoad()时, view的宽度仍为600 (由于故事板视图控制器的大小为600x600 )。 Your constraints dictate that the scroll view should be the same width as the device, but these constraints are only applied after viewDidLoad() finishes, when Auto Layout's next scheduled pass is calculated. 您的约束条件要求滚动视图的宽度应与设备的宽度相同,但是这些约束条件仅 viewDidLoad()完成后才计算自动布局的下一个计划遍次时应用。 Adding the code above forces Auto Layout to perform a pass, thus giving you the correct frame sizes for subsequent use in your size calculations. 在上面添加代码会强制自动布局执行一次传递,从而为您提供正确的帧尺寸,以供以后在尺寸计算中使用。

Put your scroll view in , set your constraint. 将滚动视图放在中,设置约束。 Then grab a UIView, put it IN the scroll view, set it as equal width, equal height, and center it (use CTRL + drag to the scroll view, you'll get a small pop up menu) 然后抓取一个UIView,将其放在滚动视图中,将其设置为相等的宽度,相等的高度,然后居中(使用CTRL +拖动到滚动视图,您将获得一个小的弹出菜单)

Then put your content in the UIView you just created... Bam ! 然后将您的内容放在刚创建的UIView中... Bam! also I personally rename my UIView to ContentView, just for sake 还是为了个人,我个人将UIView重命名为ContentView

Here's a screen to help : 这是一个帮助屏幕:

在此处输入图片说明

Now I myself am still having issue with the height, I usually simply use a fixed height for the content view because if I don't it doesn't scroll.... 现在我自己仍然对高度有疑问,我通常只对内容视图使用固定的高度,因为如果不这样做,它就不会滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM