0

The cursor in the UITextView at the top is showing up completely fine, however the second UITextView just below it places the cursor 3-4 lines below it should be. So when I type anything in it, as shown in the screenshot below, it isn't right. The code for this view controller is shown below the screenshot.

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
        }

    }

}
user2278764
  • 55
  • 1
  • 7
  • I'm no IOS developer (just OSX): Check the settings in IB they might differ. Also (OSX) `becomeFirstResponder` is deprecated. – qwerty_so Jan 23 '15 at 00:06
  • Checked settings for the both of them. Couldn't spot anything.. This is a strange issue. – user2278764 Jan 23 '15 at 00:16
  • I have experienced this as well the past view days, removing the textview and adding it to the view again (with its constrains) seemed to work. – Petri Jan 23 '15 at 08:50
  • Please have a look and the following answer, I think you need this. http://stackoverflow.com/questions/18931934/blank-space-at-top-of-uitextview-in-ios-7 – user6159419 Aug 11 '16 at 09:00

2 Answers2

0

Could it be a UILabel instead of a UITextView? UILabel tend to "center" the text and not alligning it to the left corner.

Kreuzberg
  • 894
  • 1
  • 9
  • 18
0

You probably have solved this by now, just in case I'm posting this:

self.automaticallyAdjustsScrollViewInsets = false
textView.contentOffset = CGPointZero
Arpit Dongre
  • 1,683
  • 19
  • 30