Swift - instance of backgroundColor not working - swift

If I set up an image in the Image/Image View of the Attributes Inspector the following subsequent call does not work:
picture.backgroundColor = UIColor.greenColor()
If I setup the image in the code, such as in viewDidAppear, it does.
Am I missing something, is this a quirk and how do I get it to behave?

It does work. But you can't see it in some cases.
For example: in the following image I have change the background color and set it to aspect fit. The gap between the two appears in green. So the image must have some transparency to it in order to see the background color.
Image with white background
Image with transparent background
Edit:
This is how I changed the background color
#IBOutlet weak var picture: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
picture.backgroundColor = UIColor.greenColor()
}

Related

Swift how to remove uisegmentControl default black backgroundColor?

I know a lot of people asked this question, but nothing helps. My controller1 presents a new controller2 from storyboard. Controler1's background color is black. My customized segmentcontrol background color and selectedTintColor work well, but always show a black background color first. I tried every way, but not working. In storyboard and code, all no this black color. Thanks!
override func viewDidLoad() {
let bgColor = uIColor. // my customized color)
segmentControl.backgroundColor = bgColor
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
connectionSegmentControl.selectedSegmentIndex = 0
toggleSegmentTintColor()
}
private func toggleSegmentTintColor() {
// I set my customized selected tint color here, it works.
}
If you do this in viewDidAppear, its too late: the view is on the screen. The hack way to do it is in viewDidlaod or viewWillAppear. The right way to do it is to to use UIAppearance proxy on app launch to set the tint color for your object globally:
UISegmentedControl.appearance().tintColor = UIColor.clear
You can also set the tint color for the whole app by setting it on the root window, but then it affects more than just the segmented controls.

How can I round the corners of only one side of an NSButton in Swift 4?

I am new to Swift and Stack Overflow in general, so I hope I'm not asking much to bear with me.
I am trying to achieve a 'grouped' button style that can be found on Finder or in the XCode editor toolbar, like these two button groups. As you can see in the first group of buttons, the left button is only rounded on the left side, the centre button is not rounded at all, and the right button is only rounded on the right side. The same thing applies to the second group of buttons. I want to accomplish something like this, but I'm unsure of how to achieve this.
After searching for a solution online (including iOS tutorials), I tried providing an extension to the NSButton class and manually rounding the two left corners like so:
// Extensions.swift
extension NSButton {
func roundLeftCorners() {
self.layer?.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
self.layer?.cornerRadius = 20.0 // Some arbitrary number, just wanted to make the rounded corner visible
self.layer?.masksToBounds = true
}
}
Then, on my view controller's viewDidLoad() function, I tried calling this member:
// MyViewController.swift
class MyViewController: NSViewController {
#IBOutlet weak var leftButton: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
leftButton.roundLeftCorners()
}
// ...
}
...but that didn't work for me. Some simple debugging showed that the Optional values of self.layer were nil, so I'm not sure what's going on there.
Next, I tried creating my own custom class and overriding the draw(_ dirtyRect:) function with the same code above, like so:
// LeftButton.swift
class LeftButton: NSButton {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
self.layer?.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
self.layer?.cornerRadius = 20.0
self.layer?.masksToBounds = true
}
}
// MyViewController.swift
class MyViewController: NSViewController {
#IBOulet weak var leftButton: LeftButton!
// ...
}
...but that didn't remove the rounded corners on the right side. Weirdly enough, the new cornerRadius value is only obvious if the number is around 50.0 or greater; any less and the left corners looks exactly the same as any other NSButton.
Some answers mentioned manually drawing the points in a path with NSBezierPath, but it doesn't achieve what I want. I also can't find any related properties/attributes on the Storyboard editor. Perhaps I've overcomplicated my approach to this seemingly easy problem, or maybe I'm not looking at it the right way, but I hope someone could help me with this. Thanks in advance!
1) The images you showed are using a simple NSSegmentedControl. Nothing needs customized.
2) What you tried to do wouldn't work anyway; If it could mechanically work, what it would end up doing is merely clipping the drawn content on the left corners. It wouldn't magically fill in drawing on the right, and create appropriate control borders etc.
AppKit controls are not merely CALayers with filled in properties like border, background, etc. They are almost all entirely drawn using Core Graphics via the classic drawRect: method one way or another. The fact that views have a layer is due to layer-backing. There are very few things you can end up doing with the layer of an existing control. To customize them properly, you would override the standard drawing routines in NSView, NSControl, NSCell, etc as appropriate.

Prevent image overlay in image view swift

I have a tableViewCell with image view.
Even though I clear the imageView and set the image I get the below unintended behaviour, where one image is overlaid over the other.
Please advice how this could be resolved.
Code on Resetting Image in ImageView:
self.profileImageLabelTVCell.cellImageView.image = nil // Clearing previous image
self.profileImageLabelTVCell.cellImageView.image = image
Here is the link for the tableViewCell code and roundedImageView class, that I've used. (Since SO did not allow the entire code to be posted here)
https://gist.github.com/pravishanth/14cdb8bf14dd8899b081bdc97988b985
Add your reset image code to the TableViewCell's
func prepareForReuse() method.
override func prepareForReuse() {
super.prepareForReuse()
cellImageView.image = nil
}

How can I change the background color of the arrow in an iOS popover in Swift?

When I set the background color of a UIView in the storyboard to a specific color (e.g. black) the arrow of the popup remains unchanged (e.g. white).
How can I change the color of the arrow too?
If you want to see the arrow of a popover in the same color like the background color of the popover, add the following line in the "viewDidLoad()" method in your UIViewController (the "popup"):
override func viewDidLoad() {
super.viewDidLoad()
self.popoverPresentationController?.backgroundColor = self.view.backgroundColor
}
iOS popover with changed background color

How to add vibrancy effect to an image

I made a blur image using UIBlureffect and it worked fine.
I would like to add a vibrancy image but so far it has not worked
Current code:
#IBOutlet var imageViewGif: UIImageView!
func addEffect()
{
var effect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
var effectView = UIVisualEffectView(effect: effect)
effectView.frame = CGRectMake(0, 0, 400, 536)
view.addSubview(effectView)
}
I would like to add an image that is not restricted to a preset area,
and I would like the blur effect to adapt to the image area if possible
(the image is not square).
If there is anything more you need, do not hesitate to ask!
I solved this using the hierachy view and added the "visual effect with blur and vibrancy effect" from the object library.