Mac Catalyst UITextView focus rings

Hi,


After I ported an example iPad app to the Mac with Catalyst, I noticed, that I have focus rings on my UITextFields and UITextViews, when they become first responders. The AppKit documentation says that they can be turned off in native Mac applications by seting the NSView's focusRingType to .none.


Is there a way to turn them off for UITextField and UITextView? I saw some private API on UIView like (_defaultFocusRingType, _focusRingType, _setFocusRingType:) and tried to thinker a bit and call performSelector with _setFocusRingType: and an NSNumber, but it is ineffective.


Thank you for the help in advance!


P.s. I'm aware that the focus ring helps accessibility, but the AppKit documentation states, that I can set it to none, if I use the backgroundcolor to differentiate the active text input (which is the case btw).

Replies

Wonder the same!

You've got the right idea, but you need to pass an NSUInteger by using NSInvocation:


SEL selector = NSSelectorFromString(@"_setFocusRingType:");
NSMethodSignature *signature = [self.textView methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
NSUInteger arg = 1; // NSFocusRingTypeNone
[invocation setArgument:&arg atIndex:2];
[invocation invokeWithTarget:self.textView];


Hopefully we get a real solution from Apple soon.

This will work in swift to disable the focusRing for all textViews globally.

extension UITextView {
  #if targetEnvironment(macCatalyst)
  @objc(_focusRingType)
  var focusRingType: UInt {
       return 1 //NSFocusRingTypeNone
  }
  #endif
}