我已经阅读了一些关于向UICollectionView添加标题的帖子.在Swift的iOS 7+应用程序中,我正在尝试添加一个带有UILabel的标头,其高度应根据UILabel的高度进行调整.UILabel的行= 0.
我已经使用AutoLayout在IB中设置了标题
ViewController实现UICollectionViewDelegate, UICollectionViewDataSource
.我没有为标题设置自定义类,但我正在使用这两个函数:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
//description is a String variable defined in the class
let size:CGSize = (description as NSString).boundingRectWithSize(CGSizeMake(CGRectGetWidth(collectionView.bounds) - 20.0, 180.0), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 16.0)], context: nil).size
return CGSizeMake(CGRectGetWidth(collectionView.bounds), ceil(size.height))
}
func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! {
var reusableview:UICollectionReusableView = UICollectionReusableView()
if (kind == UICollectionElementKindSectionHeader) {
//listCollectionView is an @IBOutlet UICollectionView defined …
Run Code Online (Sandbox Code Playgroud) 我有UICollectionView
类型的标题UICollectionReusableView
.
在其中,我有一个标签,其长度因用户输入而异.
因此,我需要根据标签的高度和标头中的其他元素动态调整大小.
这是我的故事板:
这是我运行应用程序时的结果:
在使用Swift编写的iOS应用程序中使用自动布局时,如何根据内容调整textField的大小?
当视图加载时以及在用户键入时,文本字段将根据需要调整大小以适合其内容.
理想情况下,文本字段将停止在特定点(例如,6行)调整大小,并变为可滚动.