NSTokenField selection list shows empty space when scrolling

When using NSTokenField , something strange happens, as shown in the figures below:

Entering A displays a selection from a popup window.

As i type a

I scrolled it

enter image description here

A few more scrolls, and he sank below the visible area.

enter image description here

This is a behavior with all table views. The view behind the lines is visible, but it automatically returns to its normal position. But not in this case.

This is normal in the Mail application, it works fine.

My implementation:

  • Created by NSTokenField .

  • Set its delegate to AppDelegate.

  • In the implementation file

      -(NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex{ return @[@"A",@"B",@"C"]; } 

Even the sample code from the Apple documentation behaves incorrectly.

How can I do this auto-spring or restrict to some code?

+4
source share
2 answers

What you see in Mail.app is not the actual NSMenu (Apple is cheating, shocking!). It turns out that the custom NSTextField is actually associated with an NSTableView stuck in a transparent window.

F-Scripting FTW

This is a pretty old trick to get around the extremely poor version of scrollWheel: NSMenu that is implemented. MTTokenField is a mature alternative to pulling your hair while trying to stick to the scroll view in NSMenu.

0
source

It is necessary to fine-tune the substring with the contents of the array. This will display exact matching records (this is a plus). Other this will avoid scrolling.

You need to change the delegation method as follows to fix the problem.

 -(NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex { NSArray *arrayContents = @[@"A",@"B",@"C"]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[cd] %@", substring]; return [NSArray arrayWithArray:[arrayContents filteredArrayUsingPredicate:predicate]]; } 

Hope this helps you.

0
source

Source: https://habr.com/ru/post/1482399/


All Articles