为什么光标不在UITextView的顶部?

时间:2015-01-22 23:46:53

标签: ios xcode swift autolayout uitextview

顶部UITextView中的光标显示完全正常,但是它下面的第二个UITextView将光标放在它下面3-4行应该是。因此,当我输入任何内容时,如下面的屏幕截图所示,这是不对的。此视图控制器的代码显示在屏幕截图下方。

Screenshot of iOS simulator showing the text in UITextView placed a couple lines lower than they should be

class AddQuestionViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var questionTextView: UITextView!
    @IBOutlet weak var answerTextView: UITextView!
    @IBOutlet weak var doneBarButton: UIBarButtonItem!
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var answerLabel: UILabel!

    weak var delegate: AddQuestionViewControllerDelegate?

    override func viewWillAppear(animated: Bool) {

        super.viewWillAppear(animated)

        self.questionTextView.layer.borderWidth = 0.5
        self.questionTextView.layer.borderColor = UIColor.lightGrayColor().CGColor
        self.questionTextView.layer.cornerRadius = 8

        self.answerTextView.layer.borderWidth = 0.5
        self.answerTextView.layer.borderColor = UIColor.lightGrayColor().CGColor
        self.answerTextView.layer.cornerRadius = 8

        self.questionTextView.becomeFirstResponder()

    }

    @IBAction func cancel() {

        delegate?.addQuestionViewControllerDidCancel(self)

    }

    @IBAction func done() {

        let item = Question()
        item.question = questionTextView.text
        item.answer = answerTextView.text
        item.checked = false
        delegate?.addQuestionViewController(self, didFinishAddingQuestion: item)

    }

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

        if textView == questionTextView {
            let oldText: NSString = questionTextView.text
            let newText: NSString = oldText.stringByReplacingCharactersInRange(range, withString: text)

            if newText.length > 0 {
                doneBarButton.enabled = true
            } else {
                doneBarButton.enabled = false
            }
        }
        return true
    }

    func textViewDidChange(textView: UITextView) {

        let temp: NSString = textView.text

        if textView == questionTextView {
            self.questionLabel.hidden = temp.length > 0
        } else {
            self.answerLabel.hidden = temp.length > 0
        }

    }

    func textViewDidEndEditing(textView: UITextView) {

        let temp: NSString = textView.text

        if textView == questionTextView {
            self.questionLabel.hidden = temp.length > 0
        } else {
            self.answerLabel.hidden = temp.length > 0
        }

    }

}

2 个答案:

答案 0 :(得分:0)

它可能是UILabel而不是UITextView吗? UILabel倾向于“居中”文本,而不是将其与左角对齐。

答案 1 :(得分:0)

你现在可能已经解决了这个问题,以防我发布这个:

self.automaticallyAdjustsScrollViewInsets = false
textView.contentOffset = CGPointZero