ios - 动态更改 View Swift 的框架

标签 ios swift uicollectionview uitextview uicollectionviewcell

enter image description here

我有一个 UICollectionViewUICollectionViewCell 包含一个 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/

相关文章:

objective-c - 如何在 xcode 4.4 中取消本地化文件

iOS 10 问题 : UIScrollView Not Scrolling, 即使设置了 ContentSize

ios - 检查 collectionView 中的文本字段是否为空

ios - 带有 viewForSupplementaryElement 的 UICollectionViewCell 中的可访问性顺序不正确

ios - 为什么建议在注册推送通知服务之前调用 registerUserNotificationSettings

swift - 以编程方式创建约束以查看 Controller 边距

ios - 将 MarqueeLabel 转换为 Swift 1.2 语法时枚举错误

ios - Swift 窗口 View 顶部的 subview

ios - 从 Collection View 中删除项目后出现标签问题

ios - 更改分组 UITableView 内单元格的角半径