1
votes

I have a programmatically created AppKit/Cocoa NSTextField, within a tab in a popup. I'm creating the control with:

NSTextField *textField = [[NSTextField alloc] init];
[textField setBezeled:YES];
[textField setDrawsBackground:YES];
[textField setEditable:YES];
[textField setSelectable:NO];
[textField setTranslatesAutoresizingMaskIntoConstraints:NO];
return textField;

And then inserting it into the parent NSView (in a tab control in a dialog box) with various layout constraints, and it appears in the correct place. However, the text field is not editable or selectable. If I change the instantiation to

[textField setSelectable:YES];

the control shows up in the tree view in the Debug View Hierarchy tool in XCode, with the same constraints that I'd expect, but the view isn't visible on screen, or in the visual display of the Debug View Hierarchy tool.

And even with [textField setSelectable:NO], I cannot edit the text that I've set in the control.

XCode Version 9.4.1 (9F2000) / macOS High Sierra 10.13.5

1
NSTextField's selectable: "If NO, the text is neither editable nor selectable". What is the size of the text field on screen?Willeke
Read the doc for isSelectable.El Tomato
Text field (when it's visible) is large enough to display the (5 or 6) digits of the floating point value I've put in the control. (And, yeah, I didn't expect it to be editable with selectable:NO, but I figured I'd try, since at least then it's visible). Is the problem that I need to add a minimum size constraint?Dan Lyke

1 Answers

1
votes

The problem was that making the field editable removed the minimum width constraint form the control. Adding a width constraint solved the problem:

[parentView addConstraint:[NSLayoutConstraint constraintWithItem:addedControl
                                                       attribute:NSLayoutAttributeWidth
                                                       relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                          toItem:nil // [button superview]
                                                       attribute:NSLayoutAttributeNotAnAttribute
                                                      multiplier:1 constant:40]];

Thank you Willeke for sending me down the right direction with the question about the size.