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

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

Related

UIStatusBar appear in black in UITabBarViewController ios11

The issue is simple :
The Application is based on UITabBarViewController
3 Tabs in the controllers
TabBar Views are configured in viewDidLoad for the
UITabBarViewController
On launch, the UIStatusBar appear in Black background color
Changing to any tab the UIStatusBar get colored!
What I'm missing?
I had a similar issue some months ago - I solved it by adding the following lines into my AppDelegate. Please find below my swift code, I'm sure you know the objective-c syntax:
// Setup general NavBar appearance
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().tintColor = UIColor.white
UIApplication.shared.statusBarStyle = .lightContent // => this would be the solution
I think the last line of code would be interesting for you. It's important to set this changes in your AppDelegate.
I had a similar issue. While not directly related the OP's issue with the tab bar, I too saw that my translucent status bar had appeared to turn opaque and was pushing the view down.
After migrating my project from Swift 3 with Xcode 8 to Swift 4 with Xcode 9, I started experiencing this same issue where the status bar appeared to be pushing content down when it was visible. At first I thought it was an issue with the preferred UIStatusBarStyle I had set (I was using UIStatusBarStyleLightContent), but that was working correctly.
The issue was that in the move from Xcode 8 to 9, a subview in one of my view controllers was misplaced in the Storyboard. A constraint that previously pinned that view to the top of the view controller was pointing incorrectly pointing to the View (with margins) instead of the Top Layout Guide. Switching this constraint to use the Top Layout Guide with a value of 0 solved my problem.
In didFinishLaunchingWithOptions method set status bar style to light content
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[application setStatusBarStyle:UIStatusBarStyleLightContent];
and in every viewController return this :
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
or if you are using the UINavigationController then also add this line of code in every viewController (or make a baseViewController) viewWillAppear method
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
For me following approach is working
final class StatusbarStyle {
static let shared = StatusbarStyle()
var style:UIStatusBarStyle = .default
}
and then whenever I need to change status bar color I write following:
StatusbarStyle.shared.style = .default
navigationController?.setNeedsStatusBarAppearanceUpdate()

How to change NSTextField background color in NSPopOver

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)

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.

Customising the font/background colour of index/section bar in UITableView

I've read in another thread on SO that you can adjust the font or background colour of the index bar (that is the A-Z # scrollbar) in a UITableView adding a CALayer. My app will be running on iOS 5 and above and since I haven't seen anything about this in the Apple docs, would it be right to say that it simply isn't possible without creating a custom table view?
If this is possible (and acceptable to Apple), how would I go about doing this?
As of iOS 6.0 there are two methods that allow you to change the color of the section indexes and the background shown when you drag along the section indexes.
if ([tableView respondsToSelector:#selector(setSectionIndexColor:)]) {
tableView.sectionIndexColor = ... // some color
tableView.sectionIndexTrackingBackgroundColor = ... // some other color
}
Of course this will only execute if the device has 6.0 or later. Under iOS 5.x, nothing will change.
Here is code for swift 2+
self.tableView.sectionIndexTrackingBackgroundColor = CustomColor
// background color while the index view is being touched
self.tableView.sectionIndexBackgroundColor = CustomColor
// title/text color of index items
self.tableView.sectionIndexColor = CustomColor
For Swift 3.1:
self.tblViewForContacts.sectionIndexTrackingBackgroundColor = self.view.backgroundColor
self.tblViewForContacts.sectionIndexBackgroundColor = self.view.backgroundColor