I'm currently practicing coding everything on my apps. So I'm not using the storyboard at all.
I've a little question which I found hard to find.
Imagine I wanna display a tableview cell like this. This one includes:
- Text (Hello)
- An image (the pencil)
- A switch (the switch)
- A button (TAP ME!)
So let's say the middle part. What is the approach to place two elements next to each other like I've done in the middle using constraints?
At the moment I've built it like this:
fileprivate func setTextConstraints() {
textToDisplay.translatesAutoresizingMaskIntoConstraints = false
textToDisplay.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
textToDisplay.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
}
fileprivate func setButtonConstraints(){
buttonToDisplay.translatesAutoresizingMaskIntoConstraints = false
buttonToDisplay.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
buttonToDisplay.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
}
fileprivate func setImageConstraints(){
imageToDisplay.translatesAutoresizingMaskIntoConstraints = false
imageToDisplay.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
imageToDisplay.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
fileprivate func setSwitchConstraints(){
switchToDisplay.translatesAutoresizingMaskIntoConstraints = false
switchToDisplay.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
switchToDisplay.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -150).isActive = true //<-------
}
I assume the "-150" is a very bad practice. So I'm looking for a good way to place two items in the center and next to each other like I've done, but in a good way.
Should I place both elements in centerXAnchor and use constant for negative and positive values? Or how else can I accomplish this in the "best" way?