macosnotificationsnswindownstextviewnsalert

NSTextView doesn’t work when placed near the top of an NSWindow


Context

I have a window with a fullSizeContentView and a transparent titlebar and hidden titlebar. I don’t even want the titlebar, but I had to enable it to get rounded corners on the NSWindow.

Problem

An NSTextView, when placed near the top edge, doesn’t react to any clicks. It doesn‘t let me select any text, and doesn’t let me click links I added via NSAttributedString.

This issue disappears when I disable the titlebar altogether.

Any Help would be greatly appreciated. Thanks!


Big Context

I’m trying to implement little “in-app notifications” that show peripheral status updates. I considered using NSAlert but I don’t want to prevent the user from interacting with the rest of the interface while the notifications are showing, so I decided to implement it myself.

The notifications are little, non-movable windows without a titlebar. They are basically just grey rectangles with rounded corners and a shadow that draw inside the main application window and contain one or a few lines of text. The first line of text is almost entirely behind the invisible titlebar which is why I’m having issues.

The only thing I need the notifications to do besides display text is link to webpages that contain more info about a notification’s message.

I feel like I might be approaching this wrong. If you have any suggestions or ideas on how to solve the problem, I’m eager to hear them. Thanks!


Solution

  • I finally figured it out!

    I nailed it down to the contentInsets of the NSScrollView. (which your are for some reason forced to have around your NSTextView when creating it in Interface Builder)
    The contentInsets were automatically being set to account for the invisible titlebar, even though the docs say that NSScrollView - automaticallyAdjustsContentInsets (which is set to YES by default and which I assume was causing this) doesn't do automatic insets for transparent titlebars.

    After programmatically setting the scrollView's contentInsets to 0, everything works great! In Objective C, you can set your scrollView's contentInsets to 0 like this:

    NSScrollView *scrollView = (NSScrollView *)self.textView.superview.superview;
    scrollView.automaticallyAdjustsContentInsets = NO; // Doesn't remove insets // Probably calling this too late
    scrollView.contentInsets = NSEdgeInsetsMake(0, 0, 0, 0);
    

    Here's a working example.

    Hope this helps!