How to change NavController's Backbutton-tintColor in Combination with SearchController in iOS13 - uicolor

As for iOS13.0 and iOS13.1, I can see a difference in behaviour when using a SearchController:
-> in iOS12.x, the BackButton is correct in Yellow
-> in iOS13.0, the BackButton is correct in Yellow
-> in iOS13.1, the BackButton is wrong (in default-Blue)
The backButton turns into the wrong color if the user types a few Search-Letters and then presses Cancel.
In addition, I set the color as follows (..not working for iOS13.1):
navigationItem.searchController?.searchBar.barStyle = .black
navigationItem.searchController?.searchBar.keyboardAppearance = .dark
navigationItem.searchController?.searchBar.tintColor = .yellow
navigationItem.searchController?.searchBar.barTintColor = .yellow

I experienced this exact issue as well and it has been fixed on iOS 13.2

Related

NSAppearance is not updating when toggling dark mode

I have a macOS app that runs only in the macOS status bar. I changed the "Application is agent (UIElement)" property in the Info.plist to "YES":
<key>LSUIElement</key>
<true/>
I have a timer that prints out the appearance's name every 5 seconds like this:
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { _ in
let appearance = NSAppearance.currentDrawing()
print(appearance.name)
}
Problem
The name doesn't actually change when I toggle dark/light mode in system settings. It always prints the name of the appearance that was set when the application launched.
Is there a way to listen to system appearance changes?
Goal
My end goal is actually to draw an NSAttributedString to an NSImage, and use that NSImage as the NSStatusItem button's image.
let image: NSImage = // generate image
statusItem.button?.image = image
For the text in the attributed string I use UIColor.labelColor that is supposed to be based on the system appearance. However it seems to not respect the system appearance change.
When I start the application in Dark Mode and then switch to Light Mode:
When I start the application in Light Mode and then switch to Dark Mode:
Side note
The reason why I turn the NSAttributedString into an NSImage and don't use the NSAttributedString directly on the NSStatusItem button's attributedTitle is because it doesn't position correctly in the status bar.
The problem with drawing a NSAttributedString is, that NSAttributedString doesn't know how to render dynamic colors such as NSColor.labelColor. Thus, it doesn't react on appearance changes. You have to use a UI element.
Solution
I solved this problem by passing the NSAttributedString to a NSTextField and draw that into an NSImage. Works perfectly fine.
func updateStatusItemImage() {
// Use UI element: `NSTextField`
let attributedString: NSAttributedString = ...
let textField = NSTextField(labelWithAttributedString: attributedString)
textField.sizeToFit()
// Draw the `NSTextField` into an `NSImage`
let size = textField.frame.size
let image = NSImage(size: size)
image.lockFocus()
textField.draw(textField.bounds)
image.unlockFocus()
// Assign the drawn image to the button of the `NSStatusItem`
statusItem.button?.image = image
}
React on NSAppearance changes
In addition, since NSImage doesn't know about NSAppearance either I need to trigger a redraw on appearance changes by observing the effectiveAppearance property of the button of the NSStatusItem:
observation = statusItem.observe(\.button?.effectiveAppearance, options: []) { [weak self] _, _ in
// Redraw
self?.updateStatusItemImage()
}

TvOS Keyboard lose keyboardAppearance after minimizing

I have an app with UISearchContainerViewController.
let searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchBar.keyboardAppearance = .dark
let container = UISearchContainerViewController(searchController: searchController)
Then I add it to screen. The keyboard look like intended. After that i minimize the application and expand it back. The keyboard become .light appearance. And no way to bring it back to .dark. How to fix that?
Before
After
Looked at https://stackoverflow.com/a/28114622/5790492
window.overrideUserInterfaceStyle = .dark
This line in AppDelegate fixed it for me.

Xcode Playground Timeline empty

I am trying to use the timeline assistant editor for playground in xcode version 7.3.1, it is always empty.
Timeline assistant editor
I think the error is from xcode, however from search it doesn't look like anyone got the same error so i am confused.
To display the result of print you need to open the "debug area" by going to menu
View > Debug Area > Show Debug Area
or click on the button in the lower left part:
To display the timeline graph, you could use XCPCaptureValue:
import XCPlayground
var x = 0
for i in 0...10 {
x += i
print(x)
XCPCaptureValue("Value for x", value: x)
}
but XCPCaptureValue has been deprecated and won't be available in the future (there's no available replacement).
The alternative is to display the graph inline by clicking on the "+" button on the right:
Do a right click on the graphs and you can choose to display value history instead:
I was just getting started in Playgrounds myself, and came across the same problem of not being able to print to Timeline.
This Medium article explains how to show or render things in the timeline as of Xcode 8 and Swift 3. Basically, you have to create a view and assign it to the PlaygroundPage.current.liveView:
import UIKit
import PlaygroundSupport
let contentView = UIView(frame: CGRect(x: 0, y: 0, width: 320.0, height: 600.0))
contentView.backgroundColor = UIColor.white
PlaygroundPage.current.liveView = contentView
Afterwards, you can add anything to your contentView to be displayed in the timeline. The PlaygroundPage.current.liveView can receive any UIView, such as UILabel, UITextField, etc.
However, sometimes the created view defaults to a black background, so you have to remember to set the .backgroundColor to UIColor.white to see it's info/child views.

How can I create a label text with shadow?

I'm trying to create a label text with a shadow created automatically by the system. In iOS I know that you can use an Attributed style for the label, but in OS X I can't find this option. Could you help?
You can do that with Core Animation (a.k.a. CALayer). Views in iOS are layer-backed by default, but NSView on Mac OS X are much older and requires you to turn on layer manually.
The easiest way is to go to the View Effects Inspector (Cmd + Alt + 8):
If you want to add the shadow programmatically:
override func awakeFromNib() {
let shadow = NSShadow()
shadow.shadowOffset = NSMakeSize(5,5)
shadow.shadowBlurRadius = 5
shadow.shadowColor = NSColor.blackColor()
myLabel.superview?.wantsLayer = true
myLabel.shadow = shadow
}

NSButton won't let me set imagePosition property

I'm trying to build a menubar application in swift. I want to set an image and text for the status item. Evidently NSStatusItem has deprecated setting a custom view since 10.10, which is fine, since I am able to set an image and text on the status item's button. However, I'm unable to set the imagePosition property for some reason and so the text and the image overlap.
This is my code:
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
func applicationDidFinishLaunching(aNotification: NSNotification) {
let icon = NSImage(named: "statusIcon")
icon!.setTemplate(true) // best for dark mode
statusItem.button!.image = icon
statusItem.button!.imagePosition = ImageLeft
statusItem.button!.title = "Hello, world"
statusItem.menu = menu;
}
The problem is that Xcode gives me an error on this line:
statusItem.button!.imagePosition = ImageLeft
It says "Use of unresolved identifier 'ImageLeft'", but from what I can tell from the documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSCell_Class/index.html#//apple_ref/c/tdef/NSCellImagePosition) that is the identifier I would want to use.
Can anyone help?
I figured it out. Evidently I have much to learn about Swift syntax.
This line allows it to work:
statusItem.button!.imagePosition = NSCellImagePosition.ImageLeft

Resources