Swift 3 - Constrain Button Width - swift

I am wanting to create a simple bar at the top of my view, with two buttons side by side, taking up 50% each.
I have created the bar like so:
let topTabView = UIView()
topTabView.backgroundColor = UIColor.red
topTabView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(topTabView)
topTabView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
topTabView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
topTabView.heightAnchor.constraint(equalToConstant: 60).isActive = true
topTabView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
And this works.
I then add my two buttons, and I get all the constraints right, except I'm not sure how to get the width anchor to work, so that each button takes up 50% of the view.
let filterButton = UIButton()
filterButton.backgroundColor = UIColor.red
filterButton.setTitle("Filter", for: .normal)
filterButton.translatesAutoresizingMaskIntoConstraints = false
topTabView.addSubview(filterButton)
filterButton.leftAnchor.constraint(equalTo: topTabView.leftAnchor).isActive = true
filterButton.centerYAnchor.constraint(equalTo: topTabView.centerYAnchor).isActive = true
filterButton.heightAnchor.constraint(equalTo: topTabView.heightAnchor).isActive = true
// NOT SURE ABOUT THIS ONE
filterButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
let mapButton = UIButton()
mapButton.backgroundColor = UIColor.red
mapButton.setTitle("Map", for: .normal)
mapButton.translatesAutoresizingMaskIntoConstraints = false
topTabView.addSubview(mapButton)
mapButton.rightAnchor.constraint(equalTo: topTabView.rightAnchor).isActive = true
mapButton.centerYAnchor.constraint(equalTo: topTabView.centerYAnchor).isActive = true
mapButton.heightAnchor.constraint(equalTo: topTabView.heightAnchor).isActive = true
// NOT SURE ABOUT THIS ONE
mapButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
I tried something like this but it didn't work:
mapButton.widthAnchor.constraint(equalToConstant: toTabView.frame.width / 2.0).isActive = true
Any help would be amazing!

so that each button takes up 50% of the view
That is what the multiplier is for. You aren't using it. Use it!
So, you want a constraint where a button's width is the same as the superview's width except with a 0.5 value for its multiplier.
Example:
let b1 = UIButton()
b1.translatesAutoresizingMaskIntoConstraints = false
b1.backgroundColor = .green
b1.setTitle("Button1", for: .normal)
b1.setTitleColor(.black, for: .normal)
let b2 = UIButton()
b2.translatesAutoresizingMaskIntoConstraints = false
b2.backgroundColor = .yellow
b2.setTitle("Button2", for: .normal)
b2.setTitleColor(.black, for: .normal)
self.view.addSubview(b1)
self.view.addSubview(b2)
NSLayoutConstraint.activate([
b1.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50),
b2.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50),
b1.leadingAnchor.constraint(equalTo:self.view.leadingAnchor),
b2.leadingAnchor.constraint(equalTo:b1.trailingAnchor),
b1.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5),
b2.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5),
])
Result:

Related

What is wrong with this StackView?

what is wrong with my StackView?
This is the code:
class PushUpViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setUpStackView()
}
func setUpStackView() {
// SetUp StackView:
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.alignment = .center
stackView.distribution = .fillProportionally
stackView.spacing = 50
view.addSubview(stackView)
// SetUp StackView Constraints:
stackView.pin(to: view)
stackView.setCustomSpacing(50, after: PushUpButton)
stackView.setCustomSpacing(100, after: TimeLabel)
// Set Elements to StackView:
stackView.addArrangedSubview(TimeLabel)
stackView.addArrangedSubview(PushUpButton)
stackView.addArrangedSubview(secondStackView)
// SetUp PushUpButton:
PushUpButton.backgroundColor = .white
PushUpButton.setTitle("\(count)", for: .normal)
PushUpButton.setTitleColor(.systemGray, for: .normal)
PushUpButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 70)
PushUpButton.translatesAutoresizingMaskIntoConstraints = false
PushUpButton.heightAnchor.constraint(equalToConstant: 300).isActive = true
PushUpButton.widthAnchor.constraint(equalToConstant: 150).isActive = true
// SetUp TimeLabel
TimeLabel.textAlignment = .center
TimeLabel.text = "\(counter)"
TimeLabel.textColor = .black
TimeLabel.font = .boldSystemFont(ofSize: 30)
self.view.addSubview(TimeLabel)
TimeLabel.translatesAutoresizingMaskIntoConstraints = false
TimeLabel.widthAnchor.constraint(equalToConstant: 300).isActive = true
TimeLabel.heightAnchor.constraint(equalToConstant: 200).isActive = true
// SetUp SecondStackView
secondStackView.translatesAutoresizingMaskIntoConstraints = false
secondStackView.axis = .horizontal
secondStackView.alignment = .center
secondStackView.distribution = .fillEqually
secondStackView.spacing = 20
// SetUp SecondStackView Constrains
secondStackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
secondStackView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -15).isActive = true
// Set Elements:
secondStackView.addArrangedSubview(breakButton)
secondStackView.addArrangedSubview(stopbutton)
//SetUp BreakButton
breakButton.backgroundColor = .lightGray
breakButton.setTitle("Break", for: .normal)
breakButton.setTitle("Start", for: .selected)
breakButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
breakButton.setTitleColor(.white, for: .normal)
breakButton.layer.cornerRadius = 12
breakButton.layer.borderWidth = 1
breakButton.layer.borderColor = UIColor.white.cgColor
// breakButton.addTarget(self, action: #selector(BreakButtonTapped), for: .touchUpInside)
breakButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
breakButton.widthAnchor.constraint(equalToConstant: 150),
breakButton.heightAnchor.constraint(equalToConstant: 50)
])
// SetUp StopButton:
stopbutton.backgroundColor = .systemRed
stopbutton.setTitle("Stop", for: .normal)
stopbutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
stopbutton.setTitleColor(.white, for: .normal)
stopbutton.layer.cornerRadius = 12
stopbutton.layer.borderWidth = 1
stopbutton.layer.borderColor = UIColor.white.cgColor
// stopbutton.addTarget(self, action: #selector(stopButtonTapped), for: .touchUpInside)
stopbutton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stopbutton.widthAnchor.constraint(equalToConstant: 150),
stopbutton.heightAnchor.constraint(equalToConstant: 50)
])
}
}
And this is how it look:
But it should looks like this:
This is what comes in the console when I am on the StackView VC:
I have no idea what this means or what I should do to solve this problem
I do not understand StackViews... I watched a lot of yt tutorials but they are all the same and did't help me. My biggest problem is the distribution of the StackView: I don't know where the difference is
First tip: forget using .fillProportionally with stack views. It is almost never used ... and when it is used, it's used for very specific reasons.
Second tip: during development, give your UI elements contrasting background colors to make it easy to see the frames at run-time.
Third tip: Use leadingLowerCase for variable and function names... Use LeadingUpperCase for class names.
Fourth tip: group similar code together - such as setting view properties, setting constraints, etc - and include logical comments to make it easier to follow what your code is doing.
Take a look at this:
class PushUpViewController: UIViewController {
let stackView = UIStackView()
let secondStackView = UIStackView()
let pushUpButton = UIButton()
let breakButton = UIButton()
let stopbutton = UIButton()
let timeLabel = UILabel()
var count: Int = 0
var counter: Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setUpStackView()
}
func setUpStackView() {
// SetUp StackView:
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .fill
// SetUp timeLabel
timeLabel.textAlignment = .center
timeLabel.text = "\(counter)"
timeLabel.textColor = .black
timeLabel.font = .boldSystemFont(ofSize: 30)
// SetUp pushUpButton:
pushUpButton.backgroundColor = .white
pushUpButton.setTitle("\(count)", for: .normal)
pushUpButton.setTitleColor(.systemGray, for: .normal)
pushUpButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 70)
// SetUp secondStackView
secondStackView.axis = .horizontal
secondStackView.alignment = .fill
secondStackView.distribution = .fillEqually
secondStackView.spacing = 20
//SetUp breakButton
breakButton.backgroundColor = .lightGray
breakButton.setTitle("Break", for: .normal)
breakButton.setTitle("Start", for: .selected)
breakButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
breakButton.setTitleColor(.white, for: .normal)
breakButton.layer.cornerRadius = 12
breakButton.layer.borderWidth = 1
breakButton.layer.borderColor = UIColor.white.cgColor
// SetUp stopButton:
stopbutton.backgroundColor = .systemRed
stopbutton.setTitle("Stop", for: .normal)
stopbutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
stopbutton.setTitleColor(.white, for: .normal)
stopbutton.layer.cornerRadius = 12
stopbutton.layer.borderWidth = 1
stopbutton.layer.borderColor = UIColor.white.cgColor
// add buttons to horizontal second stack view
secondStackView.addArrangedSubview(breakButton)
secondStackView.addArrangedSubview(stopbutton)
// if we want the center PushUpButton to be 300 x 150
// and centered vertically
// we need to embed it in a clear view
let holderView = UIView()
// add PushUpButton to holderView
holderView.addSubview(pushUpButton)
// views added as arrangedSubviews of a stack view automatically get
// .translatesAutoresizingMaskIntoConstraints = false
// but, because we're adding the PushUpButton as a subview
// of holderView, we need to set it here
pushUpButton.translatesAutoresizingMaskIntoConstraints = false
// add views to stack view
stackView.addArrangedSubview(timeLabel)
stackView.addArrangedSubview(holderView)
stackView.addArrangedSubview(secondStackView)
// add stackView to view
view.addSubview(stackView)
// SetUp StackView Constraints:
//stackView.pin(to: view)
// respect safe-area
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// constrain stackview to full view (safe-area)
// to bottom with Zero extra space
stackView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
// to top with 20-pts "padding"
stackView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
// and 8-pts "padding" on each side
stackView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 8.0),
stackView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -8.0),
// pushUpButton should be 300x150
pushUpButton.widthAnchor.constraint(equalToConstant: 300.0),
pushUpButton.heightAnchor.constraint(equalToConstant: 150.0),
// pushUpButton should be centered in holderView
pushUpButton.centerXAnchor.constraint(equalTo: holderView.centerXAnchor),
pushUpButton.centerYAnchor.constraint(equalTo: holderView.centerYAnchor),
// bottom buttons should have Height: 50
secondStackView.heightAnchor.constraint(equalToConstant: 50.0),
])
// break and stop button actions
//breakButton.addTarget(self, action: #selector(BreakButtonTapped), for: .touchUpInside)
//stopbutton.addTarget(self, action: #selector(stopButtonTapped), for: .touchUpInside)
// during development, so we can see the layout easily
//holderView.backgroundColor = .yellow
//PushUpButton.backgroundColor = .green
//TimeLabel.backgroundColor = .cyan
}
}
Result on iPhone 11:
on iPhone 8:
and with background colors to help during development:
Additional Tip:
When learning auto layout (particularly stack views), work on your layout in Storyboard / Interface Builder. You can immediately see how it looks and what happens when changing values / properties. You can also change the View as: to immediately see how it looks on different devices / screen sizes. If you want to keep everything in code, once you have your layout looking the way you want, then replicate those constraints and settings in code.

Updating constraints in swift 5

I have tried deactivating and reactivating... However I cannot seem to find out how to do it... I have code to use as an example but it keeps failing me, if anyone knows how to update a constraint in swift 5 please let me know... I know that the updateConstraint() exists just not sure how to use it.
let constraint1: NSLayoutConstraint? = text.bottomAnchor.constraint(equalTo: view.bottomAnchor)
let constraint2: NSLayoutConstraint? = text.bottomAnchor.constraint (equalTo: view.bottomAnchor, constant: 5)
if acondition {
constraint1?.isActive = false
constraint2.isActive = true
} else {
constraint2.isActive = false
constraint1.isActive = true
}
first declare the variable, after set start constraints variable to true and after call a function that active and dis active constraints, try with my example:
under controller class declare a button and a view do you want to move:
let dummyView = UIView()
let button = UIButton(type: .system)
after declare variable to move your view with constraint:
var up: NSLayoutConstraint?
var down: NSLayoutConstraint?
in ViewDiLoad set the view, the button and the constraints like this:
dummyView.backgroundColor = .yellow
dummyView.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("viewDiwn", for: .normal)
button.backgroundColor = .red
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 16)
button.addTarget(self, action: #selector(handletapButton), for: .touchUpInside)// button action that call handletapButton func
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(dummyView)
up = dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
up?.isActive = true // start constraint active
down = dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100) // constraint to activate for move the view
dummyView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
dummyView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
dummyView.heightAnchor.constraint(equalToConstant: 50).isActive = true
view.addSubview(button)
button.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
button.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
now write the function to move the view by constraint animation:
#objc func handletapButton() {
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.up?.isActive = false
self.down?.isActive = true
self.view.layoutIfNeeded()
}, completion: nil)
}
this is the result
Probably you can check your #properties and replace weak with strong. Reason is, isActive = false will set self.yourConstraint = nil, hence you wouldn't be able to use self.yourConstraint again.

swift how to remove empty space in button created by code?

How to remove empty space in button created by code? tried to play with insets and constraints but had no solution.
example:
let myButton = UIButton()
myButton.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(myButton)
myButton.backgroundColor = .lightGray
myButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
myButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
myButton.heightAnchor.constraint(equalToConstant: 50).isActive = true //this should be not hardcoded but flexible
myButton.widthAnchor.constraint(equalToConstant: 200).isActive = true //this should be not hardcoded but flexible
let downImage = UIImage(named: "caring")
myButton.imageView?.backgroundColor = .green
myButton.imageView?.contentMode = .scaleToFill
myButton.setTitle("Medium Text", for: .normal)
myButton.setImage(downImage, for: .normal)
myButton.semanticContentAttribute = .forceRightToLeft
myButton.imageEdgeInsets = .init(top: 0, left: 16, bottom: 0, right: 0)
// myButton.imageView?.translatesAutoresizingMaskIntoConstraints = false
// myButton.imageView?.widthAnchor.constraint(equalToConstant: 24).isActive = true
// myButton.imageView?.heightAnchor.constraint(equalToConstant: 24).isActive = true
// myButton.imageView?.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
// myButton.imageView?.trailingAnchor.constraint(equalTo: myButton.trailingAnchor, constant: 0).isActive = true
// myButton.contentEdgeInsets.right = 0
// myButton.contentEdgeInsets.top = 0
// myButton.contentEdgeInsets.bottom = 0
//
myButton.titleEdgeInsets.left = 16
myButton.titleEdgeInsets.right = 16
You can use below steps for this purpose.
// step 1: Setup the button
let myButton = UIButton()
myButton.translatesAutoresizingMaskIntoConstraints = false
myButton.backgroundColor = .lightGray
myButton.setTitle("Medium Text", for: .normal)
myButton.backgroundColor = .lightGray
// step 2: Set the imageView Of the button
let downImage = UIImage(named: "caring")
myButton.imageView?.backgroundColor = .green
myButton.imageView?.contentMode = .scaleToFill
myButton.setImage(downImage, for: .normal)
myButton.semanticContentAttribute = .forceRightToLeft
// step 3: Here set up the button frames and add the button to the view
myButton.sizeToFit()
self.view.addSubview(myButton)
myButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
myButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
There is no need to set the height and the width anchor which you set in your code.

UIStackView - why is the stack positioned one on top of the other?

I am trying to get a group of buttons arranged within a stack view. However, when i use a for-each loop to do this, the buttons end up being positioned one on top of the other?
var stackView: UIStackView = {
let sView = UIStackView()
sView.axis = .vertical
sView.distribution = .fill
sView.alignment = .center
return sView
}()
var optionTitles = ["Sign in", "sign out"]
for title in optionTitles{
let btn = UIButton()
btn.setTitle(title, for: .normal)
btn.backgroundColor = optionColour.withAlphaComponent(0.6)
allButtons.append(btn)
stackView.addSubview(btn)
}
// Auto layout constraints
for button in allButtons{
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: optionHeight).isActive = true
button.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true
}
view.addSubview(stackView)
stackTopConstraint = stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
stackBottomConstraint = stackView.bottomAnchor.constraint(equalTo: view.topAnchor)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackTopConstraint!.isActive = true
stackView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
2 things
1- You need to remove this
button.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true
2- Replace
stackView.addSubview(btn)
with
stackView.addArrangedSubview(btn)
You have:
stackTopConstraint = stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
stackBottomConstraint = stackView.bottomAnchor.constraint(equalTo: view.topAnchor)
You probably meant view.bottomAnchor in the second line. Surely you don't want to constraint the bottom of the stack view to the top of your view.

Cannot change label position in stackview swift

I want to change X position of a label inside a stackview, there is a button inside the stackview as well. However, I am not able to change the label's position as once I set constraints for the label, errors jump out and want me to delete the constraints.
This is an example of a stackview with a button and a label with changes to the label's x position.
import UIKit
class ViewController: UIViewController {
let stackView = UIStackView()
override func viewDidLoad() {
super.viewDidLoad()
stackView.axis = .vertical
stackView.distribution = .equalSpacing
stackView.alignment = .center
view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
stackView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
stackView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
let button = UIButton()
button.setTitle("Button", for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
button.backgroundColor = UIColor.lightGray
stackView.addArrangedSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.leftAnchor.constraint(equalTo: stackView.leftAnchor).isActive = true
button.rightAnchor.constraint(equalTo: stackView.rightAnchor).isActive = true
let label = UILabel()
label.text = "Label"
label.textColor = UIColor.black
label.backgroundColor = UIColor.lightGray
stackView.addArrangedSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
// Modify left/right anchor constraints for label x position
label.leftAnchor.constraint(equalTo: stackView.leftAnchor, constant: 24).isActive = true
label.rightAnchor.constraint(equalTo: stackView.rightAnchor).isActive = true
}
}