所以我的 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
以及superview
到scrollView
- 将
标签
的行数设置为0
(无限制)
假设您正在使用自动布局
,因为您指的是 UIScrollView content size ambiguity
关于ios - UIScrollView 需要根据 UILabel 大小进行扩展,但保持底部标签栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991624/