How to change NSTextField background color in NSPopOver - objective-c

Mac OSX 10.10 Xcode 6.1
I created a tableview in NSPopOver.
I try to change textfield's background color. Why? no effect.
The tableview's highlight set to "regular".
which way can let me change textfields background color to white?

There's a known bug with text fields and the "vibrancy" blending added in Yosemite. It's known to affect popovers.
The workaround is to set the appearance property of the table view to NSAppearanceNameAqua.
This was confirmed by an Apple engineer in their devforums.
2019-05-09 EDIT:
This issue also sometimes affects NSTextFields that appear on popovers where the background is grey. Here's the Swift 5 fix, add this to the viewDidLoad() function of your popover controller
self.someTextField.appearance = NSAppearance.init(named: .aqua)

In my app, I had same problem. I used Swift and this worked for me. In your viewForTableColumn:
let cell = tableView.makeViewWithIdentifier(tableColumn!.identifier!, owner: self) as! NSTableCellView
cell.textField?.drawsBackground = true
cell.textField?.backgroundColor = NSColor.clearColor()

I really like #Prontto's solution but it does not work with NSImageView because it has no drawsBackground or backgroundColor.
Fortunately the appearance option works for image views as well!
cell.imageView?.image = image ?? nil
cell.imageView?.appearance = NSAppearance(named: NSAppearanceNameAqua)

Related

Migration to Swift 3 from Swift 2.2 broke my custom subclassed UIButton's setting the backgroundcolor

I can't believe I cannot figure this out. After migrating to Swift 3 yesterday I got everything but one thing working.
I created a custom button subclassing from UIButton. Upon selection I want to change the background color. This worked in Swift 2.2. Now I cant change the backgroundcolor when the button's been pressed. I can still change the background color of a UIButton, but not on the subclassed button.
What changed in Swift 3, am I missing something? The color for setTitleColor does change by the way.
Code:
func selectButton(_ button: CustomButton) {
button.isSelected = true
button.backgroundColor = UIColor.red
button.setTitleColor(UIColor.white, for: .selected)
}
When I add printstatements, I see that the output is different, but the view (color) hasn't changed.
print("CHANGE COLOR FROM: \(button.backgroundColor)")
button.backgroundColor = UIColor.red
button.setTitleColor(UIColor.white, for: .selected)
button.setNeedsDisplay()
print("CHANGE COLOR TO: \(button.backgroundColor)")
Output:
CHANGE COLOR FROM: Optional(UIExtendedGrayColorSpace 1 1)
CHANGE COLOR TO: Optional(UIExtendedSRGBColorSpace 1 0 0 1)
I actually found that the problem was setting cornerRadius was somehow messing up my button. Removing the cornerRadius or changing it in viewDidAppear fixed the problem.
Also described in cornerRadius stopped working in Swift 2.3 / iOS 10 / Xcode 8

UIView background color with alpha in xib

I have a problem. I am trying to make my custom UIView with an image in the very center. The part outside of that image got to be transparent and grayed.
In xib for "View" I've set this:
The result - fail. The region outside the image is just gray, not transparent.
But if I add
-(void) viewDidLoad
{
self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
}
Then everything works as expected.
What was I doing wrong when trying to achieve transparent backround in xib?
UPDATE:
I found the code which was doing presenting of the view, and there was alpha set inside of it. So I guess that was the problem. Thanks. Sorry for being so newbie
Maybe this one is what you expected. You can only change the background Color rather the view's alpha. Just set opacity with the background Color.
When setting the alpha of a parent UIView, the alpha value will be passed to the child views, giving the same alpha to all the child views. If you set the alpha value of the UIView to 1, you are setting the alpha of the view, not the color.
That said, you can set the alpha of the background color. When you do this, the view maintains its own alpha value, while the background color has its own alpha value. Hope this helps
The issue was in custom show method. It didn't use presentViewController:animated:completion: method. It used alpha animaiton from 0 to 1. The second - when using standard presentViewController:animated:completion: method cocoa optimization was hiding the underlying view, so I need to set up (for iOS 8)
vc_to_present_on.modalPresentationStyle = UIModalPresentationCurrentContext;
vc_to_be_presented.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
And then everything work as expected

Custom UIControl with UILabel dimming on tint color change

In case of UISegmentedControl, once a popover or alert is present, the Control dims to grey (desaturates the tint color)
I'am building my own UIControl subclass, which uses a UILabel as a subview
i want to dim (desaturate) the text color of the UILabel, same way as by UISegmentedControl or (UIButton...)
Look at the tintColor and tintAdjustmentMode properties on UIView (available since iOS 7) and the tintColorDidChange method.
If you override them in your custom view you can respond to being dimmed out.
As the iOS 7 UI Transitioning Guide says:
When an alert or action sheet appears, iOS 7 automatically dims the tint color of the views behind it. To respond to this color change, a custom view subclass that uses tintColor in its rendering should override tintColorDidChange to refresh the rendering when appropriate.
The solution may look like this :
- (void)tintColorDidChange {
self.titleLabel.textColor = self.tintColor;
}
While the accepted answer did help me, the result was that the dimmed color was applied to my control even when the screen was not dimmed. I fixed this in the following manner:
override func tintColorDidChange() {
switch tintAdjustmentMode {
case .Dimmed:
myLabel.textColor = UIColor.grayColor()
default:
myLabel.textColor = UIColor.blueColor()
}
}
This correctly applies a gray color to the control only if the screen is dimmed.

Autoresizing UINavigationBar in AnnotationView

In my app I have a TabbarController. One of the tabs opens a View which starts the iPhone camera. This CameraView is part of a SDK which is developed by another company. So I can't modify the class of this CameraView.
My problem is that I have to implement a UINavigationBar in this CameraView. I've solved this through a annotationView (this navigationbar is not handled by a navigationcontroller):
[self.annotationView addSubview:navbar];
All works well except the autoresizing matter. If I turn the phone to landscape mode the navigationbar is too short.
I have already tried to set Autoresizingmask but that doesn't help.
Do you have any ideas how I can force the navigationbar to autoresize?
Thanks in advance
Try to adjust the frame of the navigationbar manually, instead of using autorotation.
-willRotateFromInterfaceOrientation:(UIInterfaceOrientation)fio toInterfaceOrientation:(UIInterfaceOrientation)tio
{
if(tio == UIInterfaceOrientationLandscape)
//set navbar frame
else
//...
}

Custom Keyboard: inputView: how to change the Keyboard size?

I implemented the textfield with a custom keyboard with the "setInputView" function.
But i have a problem: my keyboard frame is not a standard iphone keybord frame.
The question is:
How can i change the size of my custom keyboard?
I know some functions like: UIKeyboardFrameBeginUserInfoKey, ..etc.
Please Note:
The iPhone keyboard frame is = 0,264,320,216
My custom keyboard frame is = 0,0,320,460
Hoping for your kind collaboration,
Best regards...
P
It turns out that the default behaviour of the custom input view that you assign to the UITextField's property is to resize the view to the same frame as the default keyboard. Try setting (I use the name InputViewController for my input view, but you can use whatever you want):
inputViewController = [[InputViewController alloc] initWithNibName:#"InputViewController" bundle:nil];
inputViewController.delegate = self;
inputViewController.view.autoresizingMask = UIViewAutoresizingNone; // This is the code that will make sure the view does not get resized to the keyboards frame.
For more detailed information, you can look at this link, which is provided by Apple.:
If UIKit encounters an input view with an UIViewAutoresizingFlexibleHeight value in its autoresizing mask, it changes the height to match the keyboard.
Hope that Helps!
To set the keyboard inputView with the same size as the native keyboard just do this:
inputView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
To set your own frame do this:
inputView.autoresizingMask = UIViewAutoresizingNone;
From Apple:
You have a lot of flexibility in defining the size and content of an input view or input accessory view. Although the height of these views can be what you’d like, they should be the same width as the system keyboard. If UIKit encounters an input view with a UIViewAutoresizingFlexibleHeight value in its autoresizing mask, it changes the height to match the keyboard. There are no restrictions on the number of subviews (such as controls) that input views and input accessory views may have. For more guidance on input views and input accessory views, see iOS Human Interface Guidelines.
I had the same problem. I solved it by registering for UIKeyboardDidShowNotification (UIKeyboardWillShowNotification did not work, unfortunately) and then changing the view size after the keyboard was shown. However, it still had the white box on top of the keyboard when it was moving up. This worked fine for me because it is coming in over a UITextView with a white background. If you were coming in over any other colored objects, however, it would look a little ugly before the view was properly resized. You can solve that by setting the background color to clearColor.
// Add this while initializing your view
self.backgroundColor = [UIColor clearColor]; // Needed because we can't resize BEFORE showing the view. Otherwise you will see an ugly white box moving up w/ the keyboard
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
CGRect rect = self.frame;
rect.size.height = 164;
self.frame = rect;
}
Also if you're using a UIViewController to design your inputView, don't use the UIViewController.view... it seems to have a lot of problems getting resized incorrectly on rotate regardless of the AutoresizeMask.
What worked for me was to take my existing UI and use Editor > Embed In > View. Then create a new outlet, and pass that outlet as the inputView. Suddenly the resize on rotate bugs disappeared.
For me msgambel's solution didn't work. But the approach right, I was playing with the inputView's autoresizingMask. Former I had different setting, but the right way to avoid white extra space over the custom keyboard is:
I applied this just for the outermost view.