Swift sender tint color - xcode

I'm working on a tic tac toe game where each square is its own button and the image of the button changes when the square is tapped. I have done this by connecting all of the square buttons to the same IBAction #IBAction func buttonPressed(sender: AnyObject) and by using sender.setImage() to change the image. The problem is, the images are all blue. I can change this blue to a different color by changing the global tint color, but what I really want is for the O and X images to be different colors. sender.tintColor throws an error and button.tintColor, as I have in the code below, just changes the color of one image back and forth every time a square is tapped. I have tried setting the global tint to no color in the File Inspector, but it just goes back to the default blue. Any thoughts?
if activePlayer == 1 {
sender.setImage(UIImage(named: "o-img"), forState: .Normal)
activePlayer = 2
button.tintColor = UIColor.blackColor()
} else {
sender.setImage(UIImage(named: "x-img"), forState: .Normal)
activePlayer = 1
button.tintColor = UIColor.whiteColor()
}

When you drag the image to the view controller's code to connect, before clicking "Ok" change the sender to UIImage instead of AnyObject.

You need to disable the button so the next player can not tap on it.
sender.enabled = false

As it turns out, changing the button type from 'System' to 'Custom' removes the tint. Hope that helps someone else who came across the same issue!

Related

NSButton: blue background [duplicate]

This question already has answers here:
Xcode set default button on enter when making a form
(2 answers)
Closed 1 year ago.
I want to make my NSButton look like Xcode's commit button
But there doesn't seem to be any way to easily change the background color for a NSButton. You have to change the 'border' to No, then set the button's backgroundColor, and set the 'attributedTitle' for the button to make the textColor white. But when I do this, the results don't look close at all:
The button doesn't have any shadow; the text doesn't look centered; it also doesn't support changing backgroundColor when the button is selected, like the button in Xcode does.
This should surely by easy to replicate, since I believe I've seen similar buttons all over the system.
Here is the NSButton subclass I wrote for this:
override func awakeFromNib() {
super.awakeFromNib()
self.wantsLayer = true
self.isBordered = false //Important
self.layer?.backgroundColor = backgroundColor.cgColor
self.layer?.cornerRadius = 6.0
let font = NSFont.systemFont(ofSize: 14, weight: .medium)
let fontColor = NSColor.white
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
self.attributedTitle = NSAttributedString(string: self.title, attributes: [NSAttributedString.Key.foregroundColor : fontColor,
NSAttributedString.Key.font: font, NSAttributedString.Key.paragraphStyle : paragraphStyle]) // to update text, have to update the attributeString's mutableString property
}
Would love to get pointers for where I'm going wrong with this. I would have thought a regular 'push' button with borders and shadow effect is what I need, but with a blue background, but that doesn't seem straigtforward from what I can see.
Thanks.
So found the 'easy' solution I was looking for: there's no need to do any NSButton layer tweaking. You just have to set the key equivalent to the 'return' key!
[self.editButton setKeyEquivalent: #"\r"];
This sets the button background color to blue, and works as I had hoped for. If you want some other background color, I guess you have to customize the button like above.

How to scrollRowToVisible without animations

On a Mac, Mail and Finder have a solid looking scroll on their table views when the up or down arrow is held. The row highlight sits flush with the top or bottom of the column and the rows step through with no animation.
8 years ago it seems that it was hard to not do this. Now I can't seem to stop scrollRowToVisible on an NSOutlineView animating.
I have tried wrapping the call with NSAnimationContext.beginGrouping() or CATransaction.begin() etc to set any animation duration to 0.0 but no luck.
Is there anyway to make this call snap - or should I be using something a little lower level?
EDIT
Here is my code. The duration has no effect here. There are always a few frames of scroll animation, and the endpoint of the animation is slightly irregular (i.e. the bottom edge of the scrolled to view is not always aligned with the bottom edge).
if selectedRows != outlineView.selectedRowIndexes {
outlineView.selectRowIndexes(selectedRows, byExtendingSelection: false)
// I would love this not to animate like in mail, but it cannot be stopped!!!
if selectedRows.one {
NSAnimationContext.beginGrouping()
NSAnimationContext.current.allowsImplicitAnimation = false
NSAnimationContext.current.duration = 0
outlineView.scrollRowToVisible(selectedRows.first!)
NSAnimationContext.endGrouping()
}
}
Using runAnimationGroup has the same result:
NSAnimationContext.runAnimationGroup( { current in
current.allowsImplicitAnimation = false
current.duration = 0
outlineView.scrollRowToVisible(selectedRows.first!)
}, completionHandler: nil)
I have variable height rows in my table but I don't see why this would make a difference. From the above code, the change in selection is always highlighted before any movement in the table, further indication that the scroll animation is not being removed.
I had this problem myself, and solved it by subclassing NSClipView and overriding func scroll(to newOrigin: NSPoint) like this:
override func scroll(to newOrigin: NSPoint) {
super.setBoundsOrigin(newOrigin)
}
This should disable smooth scroll entirely, which is the animation effect you are describing, for the scroll view that houses your subclassed clip view.

UI Image View Animations When Using ScrollView

I am currently working on a project where I have a scroll view and four different screens. I have used a simple UIImage(named:"")!, code to put the frames of images into my imageView and animate the image.
I am having a problem where when I slide to my next screen the animation has already been completed. I do not know how to make it so that when I slide to the next screen the animation will then begin.
Here is the code for the animal animation
imageView.animationDuration = 1
imageView.animationRepeatCount = 1
imageView.animationImages =
[
UIImage(named:"Deer_00014")!,
UIImage(named:"Deer_00015")!,
UIImage(named:"Deer_00016")!,
UIImage(named:"Deer_00017")!,
UIImage(named:"Deer_00018")!,
UIImage(named:"Deer_00019")!,
UIImage(named:"Deer_00020")!,
UIImage(named:"Deer_00021")!,
UIImage(named:"Deer_00022")!,
UIImage(named:"Deer_00023")!,
UIImage(named:"Deer_00024")!,
UIImage(named:"Deer_00025")!,
UIImage(named:"Deer_00026")!,
UIImage(named:"Deer_00027")!,
UIImage(named:"Deer_00028")!,
UIImage(named:"Deer_00029")!,
UIImage(named:"Deer_00030")!,
UIImage(named:"Deer_00031")!,
UIImage(named:"Deer_00032")!,
UIImage(named:"Deer_00033")!,
UIImage(named:"Deer_00034")!,
UIImage(named:"Deer_00035")!,
]
self.imageView.image = UIImage(named: "Deer_00035")
imageView.startAnimating()
Just remove this line
self.imageView.image = UIImage(named: "Deer_00035")
Basically here you are setting the image to the ímageview' overwriting animation.

UIPopoverPresentationController's background broken?

Since iOS 9 I got a problem with the background of the UIPopoverPresentationController. It seems the shape and color are broken.
In the code below I define a popover in the prepareForSegue function and I set its preferred content size and background color. The size is shown correctly but with some mistakes in the shape of the background view. Also the color is not set (the arrow and the broken shape are white instead of dark gray (picture)). In iOS 8 it has worked as expected.
The subclassing of the UIPopoverBackgroundView should be the last option, because I just want to change the color of it, nothing else.
Any ideas?
let vc = segue.destinationViewController as! StatisticSettings_TVC
vc.preferredContentSize = CGSizeMake(300.0, 400.0)
vc.view.backgroundColor = app.COLOR_STAT_LEGEND_BACKGROUND
if let ppc = vc.popoverPresentationController {
ppc.delegate = self
ppc.backgroundColor = app.COLOR_STAT_LEGEND_BACKGROUND
}

UITabBar tint color not working on images

I have a UITabBar set up with Interface Buider and the image highlighting works correctly only if I don't set the global app tint color:
When I set the global tint color of my app using
[[UIView appearance] setTintColor:[TAStyleKit tintColor]];
then all the tab images appear as selected.
Only when I click on the tab and go back again, they have the correct color. Note the "Weapons" tab which is here grey:
What am I doing wrong ?
EDIT: Never mind what I wrote before, looks like you just need to change the following...
[[UIView appearance] setTintColor:[TAStyleKit tintColor]];
To...
[[UITabBar appearance] setTintColor:[TAStyleKit tintColor]];
Notice that you were trying to change the appearance of UIView instead of UITabBar. I ran this in a project and it did the trick.
Swift 4.0
UITabBar.appearance().tintColor = UIColor.red or TAStyleKit.tintColor
Here is may be a better one, when you can't or don't want to delete the basic UIView.tintColor code. Just set the tintColor on UIViews inside the tab bar to the default gray color.
let view = UIView.appearance()
view.tintColor = UIColor.redColor()
let viewsInTabBar = UIView.appearanceWhenContainedInInstancesOfClasses([UITabBar.self])
viewsInTabBar.tintColor = UIColor(white: 144.0 / 255.0, alpha: 1) // default gray for inactive items
let tabBar = UITabBar.appearance()
tabBar.tintColor = UIColor.redColor() // actual highlight color
unfortunately, apple does not use one of its standard gray values...
It can also be set directly from Storyboard[XCode 10.1] by setting Image Tint for UITabBar
If you are using SwiftUI. The above appearance technique won't work. Use .tint modifier on your TabView.
TabView(
...
)
.tint(Color.red)
Note that the API is .accentColor on older SwiftUI versions

Resources