简体   繁体   中英

Full Width Labels in Horizontal Scrollview with Paging

In IB I have a Scrollview (for horizontal scrolling with paging) using Auto Layout that is centered vertically and horizontally, taking up the full width of the device and has a height of 65. In my viewDidLoad, I call the below method to populate the scrollview with three labels and ideally want each to take up the full width of the device such that each scroll page has a label taking up the full width of the scrollview. I'm curious how to get rid of the magic numbers used for the x positions of the labels so that I can account for all device widths. I thought setting the x position of label2 to scrollview.frame.size.width would result in the second label starting at the right edge of the screen, however this is never the case, and I don't understand why. I've been stuck on this for the past week and would really appreciate any help. I'm using Xcode 6.1

    func generateScrollView() {

    var label1 = UILabel()
    label1.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)
    label1.text = "Label 1"
    label1.textColor = UIColor.whiteColor()
    label1.font = UIFont(name: "HelveticaNeue-Thin", size: 40.0)
    label1.backgroundColor = UIColor.greenColor()

    var label2 = UILabel()
    label2.frame = CGRectMake(320, 0, scrollView.frame.size.width, scrollView.frame.size.height)
    label2.text = "Label 2"
    label2.textColor = UIColor.whiteColor()
    label2.font = UIFont(name: "HelveticaNeue-Thin", size: 40.0)
    label2.backgroundColor = UIColor.redColor()

    var label3 = UILabel()
    label3.frame = CGRectMake(640, 0, scrollView.frame.size.width, scrollView.frame.size.height)
    label3.text = "Label 3"
    label3.textColor = UIColor.whiteColor()
    label3.font = UIFont(name: "HelveticaNeue-Thin", size: 40.0)
    label3.backgroundColor = UIColor.yellowColor()


    scrollView.contentSize = CGSize(width: 960.0, height: scrollView.frame.size.height)
    scrollView.pagingEnabled = true;

    scrollView.addSubview(label1)
    scrollView.addSubview(label2)
    scrollView.addSubview(label3)


}

Putting generateScrollView() into the viewDidAppear(animated: Bool) method, instead of viewDidLoad() did the trick for me.

To get the labels to fill the width of the device, use the width of the scrollView's frame like this:

label1.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)

Now you can set the x value of the following labels to a multiple of the scrollview's width such that the label begins at the right edge of the device.

label2.frame = CGRectMake(scrollView.frame.size.width * 2, 0, scrollView.frame.size.width, scrollView.frame.size.height)

If you're trying to center the labels in each page of the scrollview, use: label.textAlignment = NSTextAlignment.Center . This method of centering doesn't work in viewDidLoad() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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