I'm setting up constraints programmatically in Swift 3 and have a question about constraints. If I don't set a height constraint but set a topAnchor and bottomAnchor, does that do the same thing?
self.squadTableView.translatesAutoresizingMaskIntoConstraints = false
self.squadTableView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
self.squadTableView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
self.squadTableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
self.squadTableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
Compared to taking out the bottomAnchor constraint and then doing
self.squadTableView.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true
Having a top and bottom is sufficient if they are anchored to things that have sufficient constraints. You just need constraints that determine position and size, but it can be any combination that does that.
Related
I want to set a UIButton with autoLayout constraints:
Basically I want the height to be the multiplier of the container view height:
button.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.43).isActive = true
and if the height doesn't reach the constant of 44 I want to set it to 44, I added this:
button.heightAnchor(greaterThanOrEqualToConstant: 44).isActive = true
Obviously programmatically setting 2 constraints like this causes a conflict, is there a way for programmatically accomplishing these 2 prerequisites without causing a warning?
You will need to set lower priority for the "ratio" constraint
let constraint = button.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.43)
constraint.priority = .defaultHigh
constraint.isActive = true
if the engine still have troubles with figuring out the layout, you can try different priority value.
UILayoutPriority documentation
You either set a low priority of
let con = button.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.43)
con.layoutPriority //////////////
con.isActive = true
or calculate it yourself mathematically and decide with if statement
I'm new in swift, recently I want to change my heightAnchor programmatically. I have looked up some solutions on stack overflow but most of them are using IBOutlet in storyboard. My question is, if there exists a constraint already, is there any simple way to just update a new constraint?
for example
something.translatesAutoresizingMaskIntoConstraints = false
something.widthAnchor.constraint(equalToConstant: 50).isActive = true // original constraint
something.widthAnchor.constraint(equalToConstant: 100).isActive = true // want to override this one
If you have access to original constraint, hold a strong reference to it and update its constant value directly
declare a variable as
var somethingWidthConstraint:NSLayoutConstraint? = nil
and wherever you are setting this constraint originally, hold a strong reference to it
self.somethingWidthConstraint = something.widthAnchor.constraint(equalToConstant: 50)
self.somethingWidthConstraint.isActive = true
whenever you need to change its constant simply say
self.somethingWidthConstraint?.constant = 100
self.view.layoutIfNeeded()
In order for your view to accept this value and update its frame immediately (without having to wait for next UI render cycle), you might have to call self.view.layoutIfNeeded and if you wanna animate this change you can always wrap it inside UIView.animate
UIView.animate(withDuration: 0.1) {
self.view.layoutIfNeeded()
}
Thats all
Try to set the old constraint to false before setting the new one.
something.translatesAutoresizingMaskIntoConstraints = false
something.widthAnchor.constraint(equalToConstant: 50).isActive = true
something.widthAnchor.constraint(equalToConstant: 50).isActive = false
something.widthAnchor.constraint(equalToConstant: 100).isActive = true
I want to vertically centre a UILabel in a UIView. I'm using the following constraints:
layoutGuide = safeAreaLayoutGuide
header.translatesAutoresizingMaskIntoConstraints = false
titleLabel.translatesAutoresizingMaskIntoConstraints = false
header.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
header.rightAnchor.constraint(equalTo: layoutGuide.rightAnchor).isActive = true
header.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
header.heightAnchor.constraint(equalToConstant: UI.HEADER_HT).isActive = true
titleLabel.topAnchor.constraint(equalTo: header.topAnchor, constant: SPACING.LG).isActive = true
titleLabel.rightAnchor.constraint(equalTo: header.rightAnchor, constant: -SPACING.LG).isActive = true
titleLabel.leftAnchor.constraint(equalTo: header.leftAnchor, constant: SPACING.LG).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: UI.SCREEN_TITLE_HT).isActive = true
titleLabel.centerYAnchor.constraint(equalTo: header.centerYAnchor).isActive = true
The label is correctly centred but the height constraints is not being respected
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000003ade50 FC.StyledLabel:0x7fa077845330.height == 40.6 (active)>
If you want the title vertically centered, you probably don't want both topAnchor and centerYAnchor constraints. Perhaps you want to anchor that header to the label's top, instead?
Also, check out NSLayoutConstraint.activate. It will allow you to get ride of all the isActive = trues.
I'm trying to make a fully circular button. I have created it in code and setup the constraints like this:
addButton.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.2).isActive = true
addButton.heightAnchor.constraint(equalTo: addButton.widthAnchor).isActive = true
addButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -30).isActive = true
addButton.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: -30).isActive = true
I then try and set the corner radius of the button by using this:
addButton.layer.cornerRadius =
addButton.layer.masksToBounds = true
But I don't know what to put in the corner radius. I have tried frame.width / 2 but the button remains square. I need to get the value of the width constraint but I can't convert it into a CGFloat.
Any suggestions on how to do this?
Thanks
Inside viewDidLayoutSubviews put
addButton.layer.cornerRadius = self.view.frame.width * 0.1
You can either use the above answer posted by #Sh_Khan (url: https://stackoverflow.com/a/51808795/7425588) or go with the following:
addButton.layer.cornerRadius = addButton.widthAnchor.constant / 2.0
addButton.clipToBounds = true
Remember to make an UIObject complete circular, make it's height and width equal or aspect ratio to 1:1. Set clip to bounds to true and give corner radius as half of width or height.
I was following a video on youtube, running through how to build a messaging app in order to further my learning (still fairly new to Swift) The video is outdated as the constraints used are for iOS 9. The code is below:
//iOS9 Constraints for views (text bubbles)
bubbleView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
bubbleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
bubbleWidthAnchor = bubbleView.widthAnchor.constraint(equalToConstant: 200)
bubbleWidthAnchor?.isActive = true
bubbleView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
//iOS9 Constraints for views (texts)
//textView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
textView.leftAnchor.constraint(equalTo: bubbleView.leftAnchor, constant: 8).isActive = true
textView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
textView.rightAnchor.constraint(equalTo: bubbleView.rightAnchor).isActive = true
//textView.widthAnchor.constraint(equalToConstant: 200).isActive = true
textView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
What I was wondering is, where can i go to further understand how these constraints where used? How can I figure out what constraints are needed for iOS 10?