UIPopoverPresentationController's background broken? - swift2

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
}

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.

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.

Swift sender tint color

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!

Swift/OSX - Disabling Focus Ring on customized NSSecureTextField

I'm trying out some styling of NSTextFields to get Single Line Text Fields like the Material Design ones. With normal NSTextFields I have no problems, it works out pretty well. I'm subclassing the NSTextFieldCell and draw my custom cell (simply with a 1px border at the bottom).
The code is like this:
override func drawWithFrame(cellFrame: NSRect, inView controlView: NSView) {
//let path:NSBezierPath = NSBezierPath.init(rect: cellFrame)
//path.fill()
var customRect = cellFrame
customRect.origin.y = cellFrame.size.height - 1
customRect.origin.x = 0;
let usedColor:NSColor = NSColor.grayColor()
usedColor.setFill()
let path = NSBezierPath.init(rect: customRect)
path.fill()
super.drawWithFrame(cellFrame, inView: controlView);
}
I'm adding the subclass in the interface builder and assign it to my TextField(Cell). Everything works fine with normal textfields.
But now I want to do the same with NSSecureTextFields, but the outcome is weird.
The focus ring is visible, even though I set it to NONE.
The source code of the NSSecureTextFieldCell is the same as the one above (of course with the difference that I subclassed NSSecureTextFieldCell and not NSTextFieldCell), but why doesn't it show me the line at the bottom of the cell? And why do I get the damn focus ring when I assign my CustomCell-Class to the Cell? I just don't understand it and that makes me nuts.
Download Xcode Project File here (36 KB)
You can use the following code to remove the focus ring in the viewDidLoad
self.SecuredTextField.focusRingType = NSFocusRingType.None
I had the same issue and I solved it by using a standard NSTextField with a NSSecureTextFieldCell subclass as its cell class.

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