missing argument for parameter 'coder' in call Swift - swift

In my code I'm getting:
missing argument for parameter 'coder' in call swift
I'm a beginner to Swift and I've already tried everything including researching this question, but have found no answer. Thanks.
The code I'm getting this error is:
let button: UIButton = UIButton(frame: CGRect(origin: CGPoint(x: ChecklistViewController().view.frame.width / 2 + 117, y: ChecklistViewController().view.frame.size.height - 70), size: CGSize(width: 50, height: 50)))

The error is suggesting that the compiler is having trouble figuring out which init method should be invoked, so it's assuming you meant to call init(coder:).
But let's set that aside for a second. First, let's simplify your statement to eliminate some "noise". Rather than using CGRect(origin:, size:), you might use CGRect(x:, y:, width:, height:). That would yield (separating it on separate lines to make it a little easier to read):
let button = UIButton(frame: CGRect(
x: ChecklistViewController().view.frame.width / 2 + 117,
y: ChecklistViewController().view.frame.size.height - 70,
width: 50,
height: 50)
)
Second, the problem here is that that ChecklistViewController() syntax isn't actually referencing your existing ChecklistViewController. Every time it sees ChecklistViewController() it's creating a new instance of that view controller (so you probably have three instances, the original one and the two you accidentally created here). That's certainly not what you intended. If you were doing this in, one of the instance methods of the view controller itself, you'd just reference self, e.g.:
let button = UIButton(frame: CGRect(
x: self.view.frame.width / 2 + 117,
y: self.view.frame.size.height - 70,
width: 50,
height: 50)
)
A more subtle problem is that this code will only work if the frame of the view has been set. But if you have this code in viewDidLoad, the frame hasn't been set yet. If you did this in viewDidAppear, though, you could get away with this code. Generally, you'd use auto layout, to avoid this, e.g. something like:
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
// do additional configuration of the button here
view.addSubview(button)
NSLayoutConstraint.activateConstraints([
button.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor, constant: 117),
button.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor, constant: -70),
button.widthAnchor.constraintEqualToConstant(50),
button.heightAnchor.constraintEqualToConstant(50)
])
Because we did this with auto layout, it means you can do this in viewDidLoad, if you want. Also, it means that if you rotate the device, the constraints will recalculate the frame automatically for you.
Having said all of this, the "missing argument for parameter 'coder'" might be a result of some other problem in the code. But, if you fix the declaration of the button, you might be able to better diagnose any other issue that might be in the code.

I think the problem is that you are using ChecklistViewController to generate your positions.
Try this code
let button: UIButton = UIButton(frame: CGRect(x: (CGRectGetMidX(self.view.frame) + 117) , y: (CGRectGetMaxY(self.view.frame) - 70) , size: CGSize(width: 50, height: 50)))

Related

How to NSAnimationEffect in swift

I'm trying to play an animation when I remove an item from a NSTableView by dragging the item out of the window.
According to the documentation, its usually done using. NSAnimationEffect.
These effects are used to indicate that an item was removed from a
collection, such as a toolbar, without deleting the underlying data.
See NSShowAnimationEffect(::::::).
But I'm not sure how to use this.
Didn't notice that NSAnimationEffect had a show() method. That seems to do the trick.
let loc = NSEvent.mouseLocation
NSAnimationEffect.poof.show(centeredAt: NSPoint(x: loc.x, y: loc.y), size: NSSize(width: 40, height: 40))
}

How can i get the absolute position of an UISlider inside of a View

i have created a subview with a lot sliders in it
var sliderArea = UIView()
sliderArea = UIView(frame: CGRect(x: 300, y: 400, width: 500, height: 100)
view.addSubview(sliderArea)
mySlider1 = setUpSlider(sliderNr: 1, ypos: 30)
mySlider2 = setUpSlider(sliderNr: 2, ypos: 30)
mySlider3 = setUpSlider(sliderNr: 3, ypos: 30)
sliderArea.addSubview(mySlider1)
sliderArea.addSubview(mySlider2)
sliderArea.addSubview(mySlider3)
i have a lot of subviews similar to the "sliderArea" to be able to change my sliders quickly while the layout adopts automatically
now i need to know where the absolute position of each slider is to place buttons on top of it. i need to have all this buttons inside an extra view on top of it all. any ideas? thank you
You should convert your slider frame to window's coordinate system using this method of CGRect:
https://developer.apple.com/documentation/uikit/uiview/1622504-convert
Just pass nil as second parameter and it will return you an absolute frame.

Button with multiple accessible labels and images in Swift

I need to create custom button class, reuse it 4 times and I also need to override its text and image name. My next problem is how to set its frame somehow dynamically (now it is static), because I need this 4 buttons in grid 2x2.
I'm trying to create button exactly like this: https://imgur.com/a/dNhUGhc.
I have coded this but it is static and in ViewController I can't edit (override) these labels and image name. And if I tried to reuse this class I would have them in the same spot, because frame settings is exactly the same.
I'm subclassing UIButton. If something more suitable exists just let me know.
Code for adding label
// city label
let cityRect = CGRect(x: 0, y: 20, width: buttonWidth, height: 25)
let cityLabel = UILabel(frame: cityRect)
cityLabel.text = "Label"
cityLabel.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
cityLabel.textAlignment = .center
addSubview(cityLabel)
Code for adding image
// image
let imageView = UIImageView(image: UIImage(named: "something"))
imageView.frame = CGRect(x: 0, y: 60, width: 40, height: 40)
imageView.center.x = self.center.x - 20
addSubview(imageView)
Can you guys help me? Thanks
It looks like what you need to do is use an IBOutlet. Basically, an IBOutlet will give you a reference within your code (custom UIView or UIViewController subclass) to the button that you've setup in xib or storyboard. Then you can make any changes or adjustments that you want to it at runtime.
Check this out to learn more about IBOutlets and how to set them up in your project.

What is the benefit of define constant by closure in Swift 3

let a: UIView = {
let a = UIView()
a.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
return a
}()
I saw a lot of people's Swift source code defining let as this way. I just curious what is the benefit of this way?
In this case, there is no benefit, but if the variable in question were a value type then the benefit would be that you could perform some mutating setup code and still get a constant out of it.
It also lets you hide temporary variables that were only needed to initialize the constant, since they'll only exist inside the closure's scope.

Positioning a NSImage within a NSButton

I have an NSButton, created programatically. within said button, I have an NSImage that serves as an icon:
let button = NSButton(frame: NSRect(x: 10, y: (200 + (50 * id)), width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
What I'm trying to do is get the image 10pt. from the left side of the button, while centred vertically. So far, the image shows up centered horizontally as well as vertically, but since it doesn't seem like I'm able to define a frame or something like that, I can't really move it.
Is there a way to move the image within it's parent (the button)?
Thanks.
Maybe you could use the imagePosition property on a NSButton? (documented here).
It uses an enum of type NSCellImagePosition (documented here) and allows you to set the image to the left of the text, to the right of the text, above the text, below the text and so on.
You still won't have the opportunity to align things pixel perfect but if you can live with that, then imagePosition seems like the way to go.
Here is how to use it:
let button = NSButton(frame: NSRect(x: 10, y: 10, width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
button.imagePosition = .imageLeft
button.title = "Look left :)"
And that gives me this stunning UI:
Hope that helps you.