以编程方式将UITextView移动到ViewController的底部?

时间:2015-10-30 20:40:36

标签: ios swift uitableview uitextview

我以编程方式创建了textView和Button(回复),我想将其移动到ViewController的底部。它现在被置于标签(Joshyjosh)之下,它不应该这样做。有什么东西我需要添加到我的代码或更改,以便它可以移动到底部?

enter image description here

import UIKit
    class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextViewDelegate {
var commentView: UITextView?
var footerView: UIView?
var contentHeight: CGFloat = 0
let FOOTERHEIGHT : CGFloat = 50;
override func viewDidLoad() {
        super.viewDidLoad()
}

    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if self.footerView != nil {
        return self.footerView!.bounds.height
    }
    return FOOTERHEIGHT
}


func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: FOOTERHEIGHT))
    footerView?.backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha: 1)
    commentView = UITextView(frame: CGRect(x: 10, y: 5, width: tableView.bounds.width - 80 , height: 40))
    commentView?.backgroundColor = UIColor.whiteColor()
    commentView?.textContainerInset = UIEdgeInsetsMake(5, 5, 5, 5)
    commentView?.layer.cornerRadius = 2
    commentView?.scrollsToTop = true

    footerView?.addSubview(commentView!)
    let button = UIButton(frame: CGRect(x: tableView.bounds.width - 65, y: 10, width: 60 , height: 30))
    button.setTitle("Reply", forState: UIControlState.Normal)
    button.backgroundColor = UIColor(red: 155.0/255, green: 189.0/255, blue: 113.0/255, alpha: 1)
    button.layer.cornerRadius = 5
    button.addTarget(self, action: "reply", forControlEvents: UIControlEvents.TouchUpInside)
    footerView?.addSubview(button)
    commentView?.delegate = self
    return footerView
}

func textViewDidChange(textView: UITextView) {


    if (contentHeight == 0) {
        contentHeight = commentView!.contentSize.height
    }

    if(commentView!.contentSize.height != contentHeight && commentView!.contentSize.height > footerView!.bounds.height) {
        UIView.animateWithDuration(0.2, animations: { () -> Void in
            let myview = self.footerView
            print(self.commentView!.contentSize.height)
            print(self.commentView?.font!.lineHeight)
            let newHeight : CGFloat = self.commentView!.font!.lineHeight
            let myFrame = CGRect(x: myview!.frame.minX, y: myview!.frame.minY - newHeight , width: myview!.bounds.width, height: newHeight + myview!.bounds.height)
            myview?.frame = myFrame

            let mycommview = self.commentView
            let newCommHeight : CGFloat = self.commentView!.contentSize.height
            let myCommFrame = CGRect(x: mycommview!.frame.minX, y: mycommview!.frame.minY, width: mycommview!.bounds.width, height: newCommHeight)
            mycommview?.frame = myCommFrame

            self.commentView = mycommview
            self.footerView  = myview

            for item in self.footerView!.subviews {
                if(item.isKindOfClass(UIButton.self)){
                    let button = item as! UIButton
                    let newY = self.footerView!.bounds.height / 2 - button.bounds.height / 2
                    let buttonFrame = CGRect(x: button.frame.minX, y: newY , width: button.bounds.width, height : button.bounds.height)
                    button.frame = buttonFrame

                }
            }
        })

        print(self.footerView?.frame)
        print(self.commentView?.frame)
        contentHeight = commentView!.contentSize.height
    }


}

0 个答案:

没有答案