ios - UIScrollView 需要根据 UILabel 大小进行扩展,但保持底部标签栏

标签 ios swift uiscrollview

所以我的 ViewController 底部有一个自制的菜单栏,我需要将其限制在屏幕底部。在导航栏和底部栏之间,我想要一个 ScrollView ,因此我插入了一个 UIScrollView,并在该 ScrollView 中放置了一个 UIView。我这样做是因为我遇到了一个错误,该错误已通过此线程解决:

UIScrollView Scrollable Content Size Ambiguity

所以我有一些标签,可以根据标签使用的文本调整大小。我希望 UIScrollView 以及关联的 UIView 根据 UILabels 的长度进行调整。现在我的标签位于底部栏下方,但 View 没有滚动。这是我的代码:

//
//  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

    }

}

有人知道如何实现这一目标吗?任何帮助将不胜感激。

最佳答案

尝试以下步骤:

  • 设置标签的所有前导尾随顶部底部约束 到其 superview 以及 superviewscrollView
  • 标签的行数设置为0(无限制)
<小时/>

假设您正在使用自动布局,因为您指的是 UIScrollView content size ambiguity

关于ios - UIScrollView 需要根据 UILabel 大小进行扩展,但保持底部标签栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991624/

相关文章:

ios - 使用适用于 iOS 的 OpenGL ES 2.0 的线性代数库

swift - 使用 swift 在 mailcore2 OSX 中获取电子邮件正文

ios - 根据选择的初始值显示 JSON 数据

ios - 较小时 UIScrollView 的中心内容

ios - 从所选视频中获取图像 UIImagePickerController

iphone - 绘制图像缩放并旋转后,到其他图像iOS

android - Flutter In App Purchase : InAppPurchaseConnection. instance.queryProductDetails 返回空列表

ios - 使用 GoogleMaps SDK 生成路线?

ios - 选择表格单元格时删除键盘

ios - UIScrollView subview 中的 UILabels 上的 UITapGestureRecognizer 不起作用