21

I was learning about the view debugger in Xcode and capturing the view hierarchy with Debug > View Debugging > Capture View Hierarchy. However when I tried it in my app I got the following error:

Assertion failure in -[UITextView _firstBaselineOffsetFromTop], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UITextView.m:1683

I could reproduce this in the following simple project:

import UIKit
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let myTextView = UITextView()
        myTextView.frame = CGRect(x: 50, y: 50, width: 200, height: 100)
        myTextView.text = "This is a test."
        view.addSubview(myTextView)

    }
}

I saw here that it could be caused by not using auto layout. Is this really a bug that we have to wait for a fix? Is there a Swift workaround?

Update

The suggested duplicate looks like the same issue I am having. However, unlike that question, I am asking for a Swift workaround. The "answer" to that question was just a link (the same link that I already had above). I am voting to close the other way.

1

1 Answer 1

46

Note do this in DEBUG builds only

A workaround to resolve this issue. Keep below category in your project. It worked for me.

@interface UITextView(MYTextView)

@end

@implementation UITextView (MYTextView)
- (void)_firstBaselineOffsetFromTop {

}

- (void)_baselineOffsetFromBottom {

}

@end

For swift

extension UITextView {
    func _firstBaselineOffsetFromTop() {
    }
    func _baselineOffsetFromBottom() {
    }
}
3
  • 1
    Ah, I understand now. So this is overriding the methods that were causing the assertion failure. I suppose if those methods were doing anything else important you are losing that functionality, but this is a very useful workaround when needing to capture the view hierarchy. The extension (or category) could then be commented out later. (I tested the Swift code with the sample project in my question and I can confirm that it does work.)
    – Suragch
    May 12, 2016 at 23:04
  • 3
    Enable in debug mode only by adding this to your .pch file #ifdef DEBUG #import "UITextView+MYTextView.h" #endif
    – jpredham
    Jul 19, 2016 at 16:55
  • Thanks for the clever workaround && Nice Job Apple >=:
    – Amr Lotfy
    Feb 10, 2017 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.