1

So I have a self made menu bar at the bottom of my ViewController that I need to keep constrained to the bottom of the screen. Between the navigation bar and the bottom bar I want a scroll view so I inserted a UIScrollView and within that scroll view I put a UIView. I did this because I was getting an error that was addressed with this thread:

UIScrollView Scrollable Content Size Ambiguity

So I have some labels that adjust in size based upon the text being used for the labels. I want the UIScrollView and also the associated UIView to adjust based upon how long the UILabels are. Right now I have it where the labels go below the bottom bar but the view isn't scrolling. Here is my code:

//
//  ItemViewController.swift
//

import UIKit
import Parse

class ItemViewController: UIViewController {


    @IBOutlet weak var menuButton: UIButton!

    @IBOutlet weak var mainImage: UIImageView!

    @IBOutlet weak var innerView: UIView!

    @IBOutlet weak var scrollView: UIScrollView!


    @IBOutlet weak var heading1: UILabel!

    @IBOutlet weak var body1: UILabel!

    @IBOutlet weak var heading2: UILabel!

    @IBOutlet weak var body2: UILabel!




    override func viewDidLoad() {
            super.viewDidLoad()


            for var y = 0; y < detailsHeadings.count; y++ {

                    switch y {
                    case 0:
                            heading1.text = detailsHeadings[y]
                            body1.text = details[heading1.text!]
                            let numLines = calcLines(body1.text!)
                            body1.numberOfLines = numLines!
                            break;
                    case 1:
                            heading2.text = detailsHeadings[y]
                            body2.text = details[heading2.text!]
                            let numLines = calcLines(body2.text!)
                            body2.numberOfLines = numLines!
                            break;
                    default:

                            break;
                    }

            }

            //none of the below worked
            scrollView.sizeToFit()
            scrollView.sizeThatFits(CGSize(width: 310, height: 700))
            innerView.sizeThatFits(CGSize(width: 310, height: 700))

            innerView.frame = CGRect(x: 0, y: 0, width: 320, height: 700)


    }

    override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
    }



    func calcLines(textString: String) -> Int?  {

            let text = textString
            // cast text to NSString so we can use sizeWithAttributes
            var myText = text as NSString

            //Set attributes
            var attributes = [NSFontAttributeName : UIFont.systemFontOfSize(12)]

            //Calculate the size of your UILabel by using the systemfont and the paragraph we created before. Edit the font and replace it with yours if you use another
            var labelSize = myText.boundingRectWithSize(CGSizeMake(body1.bounds.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil)

            //Now we return the amount of lines using the ceil method
            var lines = ceil(CGFloat(labelSize.height) / body1.font.lineHeight)
            println(lines)

            let linesInt: Int = Int(lines)

            return linesInt

    }

}

Does anyone have any idea how to achieve this? Any help would be greatly appreciated.

Community
  • 1
  • 1
Joe
  • 157
  • 1
  • 12
  • 1
    The easiest method to achieve this is not to program everything by hand: use a storyboard, use autolayout, define all constraints necessary top to bottom, and let the OS calculate the size of your enclosure. – SwiftArchitect Nov 30 '15 at 05:00

1 Answers1

0

Try these steps:

  • Set all leading, trailing, top, and bottom constraints of your label to its superview and the superview's to the scrollView
  • Set your label's number of lines to 0 (unlimited)

Assuming that you're using autolayout since you're referring to UIScrollView content size ambiguity

Community
  • 1
  • 1
Islam
  • 3,654
  • 3
  • 30
  • 40