如何调整UITextView的大小?迅速

时间:2015-02-11 18:26:18

标签: ios iphone swift

我的应用有问题,请看这张照片

this pic

现在我在textView上打字,我想要一种方法来调整textview的大小或将其移到顶部或滚动到最后一个我打字的地方请帮忙

我在这里看到很多答案,但都没有和我合作,这是我尝试其中一个答案时的代码

import UIKit

class AddEditClassNoteViewController: UIViewController {

    @IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint?


    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    @IBOutlet weak var noteTextBox: UITextView!

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
    {

   self.noteTextBox.resignFirstResponder()




    }





        func textFieldDidBeginEditing(textField: UITextField) {
            animateViewMoving(true,moveValue: 100)
        }
        func textFieldDidEndEditing(textField: UITextField) {
            animateViewMoving(false,moveValue: 100)
        }

    func animateViewMoving (up:Bool, moveValue :CGFloat){
        var movementDuration:NSTimeInterval = 0.3
        var movement:CGFloat = ( up ? -moveValue : moveValue)
        UIView.beginAnimations( "animateView", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration )
        self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
        UIView.commitAnimations()
    }






}

我正在使用Swift IOS 8

2 个答案:

答案 0 :(得分:0)

好像你要求几件事,(我猜,所有这些,已经有Stack Overflow的答案),但我会尽力给你一些例子。

首先,当你点击视图时,你到目前为止似乎隐藏了键盘。这可能部分地违背了你的目的。

其次,如果您尝试滚动到noteTextBox的顶部,请尝试使用: noteTextBox.setContentOffset(CGPointMake(0, 0), animated: true)
如果您想要滚动动画,请使用animted: true。你可能没有,我不知道]

第三,请查看此问题,以便调整UITextViewClick Here

的大小

第四,看看这个问题,回到UIView上的“记住”位置:Click Here

祝你好运,
凯尔

答案 1 :(得分:0)

任何人都需要使用Swift语言的答案,答案仅如下所示。

import UIKit

class AddEditClassNoteViewController: UIViewController {

    var keyboardShowing = false
    @IBOutlet weak var noteTextBox: UITextView!


    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardShow:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardHide:", name: UIKeyboardWillHideNotification, object: nil)


        // Do any additional setup after loading the view.
    }
    override func shouldAutorotate() -> Bool {
        return !self.keyboardShowing
    }

    // much simpler than in iOS 7; a lot of the touchy bugs are gone in iOS 8
    // as long as you play your part (adjust content offset),
    // iOS 8 will play its part (scroll cursor to visible)
    // and we don't have to animate




    func keyboardShow(n:NSNotification) {
        self.keyboardShowing = true

        let d = n.userInfo!
        var r = (d[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()
        r = self.noteTextBox.convertRect(r, fromView:nil)
        self.noteTextBox.contentInset.bottom = r.size.height
        self.noteTextBox.scrollIndicatorInsets.bottom = r.size.height
    }

    func keyboardHide(n:NSNotification) {
        self.keyboardShowing = false
        self.noteTextBox.contentInset = UIEdgeInsetsZero
        self.noteTextBox.scrollIndicatorInsets = UIEdgeInsetsZero
    }

    func doDone(sender:AnyObject) {
        self.view.endEditing(false)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }


    override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
    {

   self.noteTextBox.resignFirstResponder()




    }







}

感谢所有想帮助我的人。