我有一个 UICollectionView
。 UICollectionViewCell
包含一个 UITextView
。我想动态更改 UITextView
的框架。我使用以下代码。问题是有时这行得通,有时却行不通,并且没有对框架进行任何更改。我不确定是什么问题。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("message_cell" , forIndexPath: indexPath) as! DisplayMessageCollectionViewCell
if let messageText = "testing" {
let size = CGSizeMake(250, 1000)
let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
let estimatedFrame = NSString(string: messageText).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)], context: nil)
if let user_id = NSUserDefaults.standardUserDefaults().stringForKey("userId") {
if (user_id == "testing") {
cell.messageTextView.frame = CGRectMake(view.frame.width - estimatedFrame.width - 16 - 8, 0, estimatedFrame.width + 16, estimatedFrame.height + 20)
}
else {
cell.messageTextView.frame = CGRectMake(48 + 3, 0, estimatedFrame.width + 15, estimatedFrame.height + 20 )
}
}
}
return cell
}
最佳答案
讨论 Here ,
将尾随和前导 NSLayoutConstraints 添加到 messageTextView
解决了这个问题
关于ios - 动态更改 View Swift 的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37512179/