Change Background Color of button in OSX - swift

I want to change the background color of a button in a dialog to red, if a specific option is enabled. I tried
run.backgroundColor = NSColor.red
but nothing happens. (run is the name of the button) There are similar questions, but no answers. And these questions are years old. I hope there is a simple solution, bc. this seems not an unusual approach. Again, it belongs to OSX (and SWIFT 3).

NSButton doesn't have a backgroundColor property, but it's CALayer does. So, to get the same effect you could do the following:
myButton.wantsLayer = true
myButton.layer?.backgroundColor = NSColor.red.cgColor

Related

Swift macOS | Setting (switching between) dark and lightmode

I am working on a macOS app where I want to give users the ability to switch between light and dark mode.
For iOS apps, this can be done by simply overriding the UserInterfaceStyle of UIWindow. Like so:
window.overrideUserInterfaceStyle = .dark //.light
The problem: NSWindow doesn't have a UserInterfaceStyle property.
I have tried to set the NSAppearance
window.appearance = NSAppearance(appearanceNamed: NSAppearance.Name.aqua, bundle: nil)
without success. It returns me an error saying: "RunTimeThemeRefForBundleIdentifierAndName() couldn't find NSAppearanceNameAqua.car in bundle with identifier: ..."
I am stuck. Do you have any ideas?
Here would be a solution for iOS
Thanks!
I was on the right track!
window.appearance = NSAppearance(named: .aqua)
worked.
There is actually a guide to set the appearance of macOS apps from apple.
Note: This also works if you have all your content inside a NSPopover, since it too has a property appearance.

Additional ColorScheme in Swift/SwiftUI beside Dark Mode and Light Mode

I'm working on an App that usually will be used in dark environments and the light output from the display should be reduced to a minimum. So I want to have a low-contrast with really black backgrounds and grey text.
If someone is using that App in another environment the user might consider a usual view with higher contrast - so for example the usual dark or light mode.
Of course I can set all backgrounds to black and foregroundColors to gray, but that would require a lot of code to give the user the possibility back to change to usual light / dark mode.
MY approach was rather to write an extension to ColorScheme, starting with
public extension ColorScheme {
static let evenDarker = ColorScheme.dark
}
and adjusting the colorvalues from there.
In my imaginiation this should have created a copy of .dark. I can call
let contentView = ContentView()
.environment(\.colorScheme, .evenDarker)
and it builds without problems, unfortunately it does show the system-setting (as defined on the device) and not a copy of the dark mode. Calling the same view with .dark or .light overrides the system setting though.
Besides I can't find any list or documentation of possible values in the ColorScheme - e.g. how to override the default background color from gray to black.
Did anyone ever get this or something similar get to work?
In the meantime I found a solution that works for the first question:
Working with
public extension UIUserInterfaceStyle {
static let evenDarker = UIUserInterfaceStyle.dark }
and
window.overrideUserInterfaceStyle = .evenDarker
works better, it copies .dark to .evenDarker - and different from using the environment-setting of the ContentView() it also has an option to use .unspecified - if the user wants to use the system setting.

autocorrectType is not working on iOS 13 using swift

I know this is quiet simple and straight forward to disable autocorrection on iPhone and it worlds pretty much fine, but until now. Surprisingly, suggestion bar appear for text field even I have disabled it from storyboard and even programmatically.
self.textField.autocorrectionType = .no
Done that in sotoryboard as well.
But it does appear on iOS 13, not on previous versions and simulator.
There are many answers already for this one but none is working on iOS 13 so no need to mark the question as duplicate.
Cheers!
If you are using a Storyboard you can disable autocorrection on the attributes inspector (the down arrow icon) under the Text input traits. See the image below for details.

Caret cursor color and size

How do I change the size and colour of the flashing cursor in my textfield using my storyboard for my Mac app.
I assume I have to connect it into the view controller script (control drag) and edit some uitextfield color parameter for the cursor?
But I don't seem to be getting anywhere fast. I am working in swift 2.2 with Mac app Storyboards. Like Ulysses and Taskpaper.
In Swift 3:
Change YOURTEXTFIELD to your textfield and you are set:
(YOURTEXTFIELD.value(forKey: "textInputTraits") as AnyObject).setValue(UIColor.black, forKey: "insertionPointColor")
Documentation: https://developer.apple.com/documentation/appkit/nstextview/1449309-insertionpointcolor
Search for _drawInsertionPointInRect that's the private method that you need. Though you also need some other stuff I think... but it's all mixed into my code so I can't say for sure what it all is. Anyway search for _drawInsertionPointInRect and you'll get to some explanations.

Xcode & Swift - Window without title bar but with close, minimize and resize buttons

I am currently using Swift in Xcode 6, Beta 5.
I am trying to remove the title bar, or any visible difference between the title bar and the actual content. If I enable "Unified title and toolbar" in the Attributes Inspector on a Window, nothing visibly happens. I have already left the title out.
When no title is entered, the title bar will still be distinguishable because of the border line and background difference with the rest of the window, separating it from the actual content.
An excellent example would be the current Yosemite, OS X 10.10, Notes app. No title bar is visible or distinguishable, just the Close, Minimise and Resize buttons as seen here.
I have searched and visited other posts, but to no to little avail.
Those mentioned hiding the title bar altogether, but I wouldn't know how to manually re-add the Close, Minimise and Resize buttons properly, meaning they would look correct, no actual, sneaky image replacements and connections with the menu bar Close, Minimise and Resize functions.
The new window style mask NSFullSizeContentViewWindowMask added in OS X 10.10 will do the trick.
self.window.titleVisibility = NSWindowTitleVisibility.Hidden;
self.window.titlebarAppearsTransparent = YES;
self.window.styleMask |= NSFullSizeContentViewWindowMask;
Release Notes
Since MacOS X 10.10, you can use these:
if #available(macOS 10.10, *) {
window.titlebarAppearsTransparent = true
}
if #available(macOS 10.2, *) {
window.movableByWindowBackground = true
}
There was an official sample project for window appearance in Yosemite. You might wanna check it out.
For Swift 3 :-
self.window.titleVisibility = .hidden
self.window.titlebarAppearsTransparent = true
self.window.styleMask.insert(.fullSizeContentView)
If you are using storyboard, it's just a simple check box in the Inspector bar.
Select the window from Story Board
Check the Transparent Title Bar checkbox in the inspector window.
Here's how it looks like in the Story board. It looks the same when you build and run the application.
You can use these:
override func viewDidAppear() {
super.viewDidAppear()
self.view.window?.titlebarAppearsTransparent = true
self.view.window?.movableByWindowBackground = true
}
Update Sept. 2017, taget 10.11:
override func viewDidAppear() {
super.viewDidAppear()
self.view.window?.titleVisibility = .hidden
self.view.window?.titlebarAppearsTransparent = true
self.view.window?.styleMask.insert(.fullSizeContentView)
}
I don't have enough reputation to comment on Ranfei Songs answer, but running on OSX 10.12 the syntax for the titleVisibility is slightly different, instead of this:
self.window.titleVisibility = NSWindowTitleVisibility.Hidden;
you'll need to use NSWindowTitleHidden instead, so updating Ranfei's code would result in you need to specify this like this:
self.window.titleVisibility = NSWindowTitleHidden;
self.window.titlebarAppearsTransparent = YES;
self.window.styleMask |= NSFullSizeContentViewWindowMask;