Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据文本内容调整文本视图的大小(swift)_Swift - Fatal编程技术网

根据文本内容调整文本视图的大小(swift)

根据文本内容调整文本视图的大小(swift),swift,Swift,我看到一些关于调整文本视图大小的帖子。然而,在应用这些代码之后,我没有做到这一点。请帮忙。任何帮助都将不胜感激。将委托设置为uitextview func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellFrame = CGRectMake(0, 0, tableView.frame.width, 100)

我看到一些关于调整文本视图大小的帖子。然而,在应用这些代码之后,我没有做到这一点。请帮忙。任何帮助都将不胜感激。

将委托设置为uitextview

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cellFrame = CGRectMake(0, 0, tableView.frame.width, 100)
    var retCell = UITableViewCell(frame: cellFrame)
     var textView = UITextView(frame: CGRectMake(8,30,tableView.frame.width-8-8,65))
                textView.layer.borderColor = UIColor.blueColor().CGColor
                textView.layer.borderWidth = 1
                                    retCell.addSubview(textView)
                                    if(reqGrpIndex == 0){
                    reqGrpIndex = reqGrpIndex + 1
                }
}

func textViewDidChange(textView: UITextView){
    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    var newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    textView.frame = newFrame;
}

您是否设置了textview
委托
?没有。我没有。如何设置其授权。textView.delegate=self??是。因此,它将反映委托方法,如
textViewDidChange
。参考此项,但它提示了一个错误“无法将“ViewController”类型的值分配给“UITextViewDelegate”类型?”是否已将
UITextViewDelegate
?添加为
class YourViewController:UIViewController,UITextViewDelegate
//If your class is not conforms to the UITextViewDelegate protocol you will not be able to set it as delegate to UITextView
class YourViewController: UIViewController, UITextViewDelegate { 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        textView.delegate = self
    }

    func textViewDidChange(textView: UITextView) { 
        // your resize things
    }
}