How to align two uibuttons in storyboard - swift

how do i make two button "Like" and "Comment" with equal space on the left of "Like", equal space in the middle of "Like" and "Comment", and equal space on the right of "Comment" depending on the phone size
here is an image example:

Try the following code it is a bit different approach but hope it will work for you. It is better to set these constraints directly in Storyboard.
let deviceWidth = UIScreen.mainScreen().bounds.size.width
let buttonWidth = 75
let equalPadding = (deviceWidth - (2 * buttonWidth))/3
let centerXOfLikeButton = -(buttonWidth/2 + equalPadding/2)
let centerXOfCommentButton = (buttonWidth/2 + equalPadding/2)
// Like button constraints
let likeBtnTopConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 20)
let likeBtnWidthConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: buttonWidth)
let likeBtnHeightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 40)
let likeBtnXConstraint = NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: centerXOfLikeButton)
self.view.addConstraint(likeBtnTopConstraint)
self.view.addConstraint(likeBtnWidthConstraint)
self.view.addConstraint(likeBtnHeightConstraint)
self.view.addConstraint(likeBtnXConstraint)
// Comment button constraints
let commentBtnTopConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 20)
let commentBtnWidthConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: buttonWidth)
let commentBtnHeightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 40)
let commentBtnXConstraint = NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: centerXOfCommentButton)
self.view.addConstraint(commentBtnTopConstraint)
self.view.addConstraint(commentBtnWidthConstraint)
self.view.addConstraint(commentBtnHeightConstraint)
self.view.addConstraint(commentBtnXConstraint)
Using storyboard, please refer screenshots provided below

Crate Two button with equal width and equal height and give "titleEdgeInsets" on each button as bellow,
let insert = likeButton.frame.width / 4
commentButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: insert)
likeButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: insert, bottom: 0, right: 0)

Related

Properly captioning aspect fit images

I am looking for the proper way to add a caption to the top and bottom of any image in an imageview set to aspect fit. Seems simple enough, but the textbox doesnt seem to move most of the time and ends up in the center other times. Here is an example of a formula I tried for the top caption.
mainTextBox.center.y = mainImageView.center.y + (mainImageView.image!.size.height / 2) - (mainTextBox.frame.size.height / 2)
Updated with help from Vandan Patel:
I have also tried manually setting constraints manually buy receive an error
let titleLabelWidth = NSLayoutConstraint(item: mainTextBox, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 0.9, constant: 0)
let titleLabelCenterX = NSLayoutConstraint(item: mainTextBox, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
let titleLabelCenterY = NSLayoutConstraint(item: mainTextBox, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
let titleLabelHeight = NSLayoutConstraint(item: mainTextBox, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 0.9, constant: 0)
mainTextBox.addConstraints([titleLabelWidth, titleLabelCenterX, titleLabelCenterY, titleLabelHeight])
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:mult‌​iplier:constant:]: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs'
Are you looking for something like this?
//title-Label Constraints
let titleLabelWidth = NSLayoutConstraint(item: titleLabel, attribute: .width, relatedBy: .equal, toItem: blurEffectView, attribute: .width, multiplier: 0.9, constant: 0)
let titleLabelCenterX = NSLayoutConstraint(item: titleLabel, attribute: .centerX, relatedBy: .equal, toItem: blurEffectView, attribute: .centerX, multiplier: 1, constant: 0)
let titleLabelCenterY = NSLayoutConstraint(item: titleLabel, attribute: .centerY, relatedBy: .equal, toItem: blurEffectView, attribute: .centerY, multiplier: 1, constant: 0)
let titleLabelHeight = NSLayoutConstraint(item: titleLabel, attribute: .height, relatedBy: .equal, toItem: blurEffectView, attribute: .height, multiplier: 0.9, constant: 0)
self.addConstraints([titleLabelWidth, titleLabelCenterX, titleLabelCenterY, titleLabelHeight])

Swift UIWebView constraints full screen

I have these constraints for my UIWebView:
let horizontalConstraint = NSLayoutConstraint(item: webview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: webview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
view.addConstraint(verticalConstraint)
let widthConstraint = NSLayoutConstraint(item: webview, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
view.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: webview, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
view.addConstraint(heightConstraint)
My question is, how do I get the width and height constraint to be full device screen? I have to do this programmatically
Use this constraints for a full screen mode:
let topConst = NSLayoutConstraint(item: self.webView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
let botConst = NSLayoutConstraint(item: self.webView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let leftConst = NSLayoutConstraint(item: self.webView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)
let rigthConst = NSLayoutConstraint(item: self.webView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([topConst,botConst,leftConst,rigthConst])
self.webView.translatesAutoresizingMaskIntoConstraints = false
webview is the view that I wanted to be in full screen. Don't forget to set the translatesAutoresizingMaskIntoConstraints to false after activate the constraints.

After adding an NSLayoutConstraint, view disappears

When I add an NSLayoutConstraint to a view, it results in the view disappearing.
I am using the following code:
let test:UIView = UIView(frame: CGRectMake(0, 0, 100, 100))
test.backgroundColor = UIColor.blackColor()
self.view.addSubview(test)
test.translatesAutoresizingMaskIntoConstraints = false
let topCKCtr = NSLayoutConstraint(item: test, attribute: .CenterX, relatedBy: .Equal, toItem: test.superview, attribute: .CenterX, multiplier: 0.5, constant: 0)
topCKCtr.active = true
let topCKCtr1 = NSLayoutConstraint(item: test, attribute: .CenterY, relatedBy: .Equal, toItem: test.superview, attribute: .CenterY, multiplier: 0.5, constant: 0)
topCKCtr1.active = true
self.view.layoutIfNeeded()
self.view.setNeedsDisplay()
When I debug view hierarchy, i see that the view exists, even though it is not visible. See the below screenshot for details - only the constraint is visible, not the view:
There are so many things that needs to be discussed here.
When you use the following
test.translatesAutoresizingMaskIntoConstraints = false
Then it will totally rely on constraints to position and size a view.So you need to set height and width constraints of the view.
let topCKCtr2 = NSLayoutConstraint(item: test, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1.0, constant: 100)
let topCKCtr3 = NSLayoutConstraint(item: test, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 100)
Finally This will be the code which you are looking for
let test:UIView = UIView(frame: CGRectMake(0, 0, 100, 100))
test.backgroundColor = UIColor.blackColor()
self.view.addSubview(test)
test.translatesAutoresizingMaskIntoConstraints = false
let topCKCtr = NSLayoutConstraint(item: test, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 0.5, constant: 0)
topCKCtr.active = true
let topCKCtr1 = NSLayoutConstraint(item: test, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 0.5, constant: 0)
topCKCtr1.active = true
let topCKCtr2 = NSLayoutConstraint(item: test, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1.0, constant: 100)
topCKCtr2.active = true
let topCKCtr3 = NSLayoutConstraint(item: test, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 100)
topCKCtr3.active = true

Auto layout issue in swift code

want to create UI such that constraint should be such that the space between the two button and the distance from the side edge should be same.
I am using the following code
func addConstraintsToBottomButtons()
{
let viewFrame = self.view.frame
let availableWidth:CGFloat = viewFrame.width - 60
let buttonDistance:CGFloat = availableWidth/3
let buttonWidth:CGFloat = 30.0
let buttonHeight:CGFloat = 30.0
var BtnOne = UIButton()
BtnOne.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(BtnOne)
BtnOne.backgroundColor = UIColor.redColor()
var btnTwo = UIButton()
btnTwo.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(btnTwo)
btnTwo.backgroundColor = UIColor.yellowColor()
//Views to add constraints to
//Metrics for Visual Format string
// Button One Constraint..
self.view .addConstraint(NSLayoutConstraint(item: BtnOne, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: bottonView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: buttonDistance))
self.view .addConstraint(NSLayoutConstraint(item: BtnOne, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: bottonView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
self.view .addConstraint(NSLayoutConstraint(item: BtnOne, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: buttonWidth))
self.view .addConstraint(NSLayoutConstraint(item: BtnOne, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: buttonHeight))
// Button Two Constraint...
self.view .addConstraint(NSLayoutConstraint(item: btnTwo, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: bottonView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -(buttonDistance)))
self.view .addConstraint(NSLayoutConstraint(item: btnTwo, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: bottonView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
self.view .addConstraint(NSLayoutConstraint(item: btnTwo, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: buttonWidth))
self.view .addConstraint(NSLayoutConstraint(item: btnTwo, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: buttonHeight))
}
let me suggest a workaround that will do just the thing you want:
Create three views in storyboard / pull them out of the same place where you get buttons - and use them as cushions between the edges of your screen and your buttons: anchor to edges, equal width, and lower horizontal compression than the actual buttons and it should behave as desired.

Swift Assign Button Position from Plist

I have Add Constraint in My func
func CreateButtonWithIndex(var index:Int) {
let path = loadPath()
var data = NSMutableDictionary(contentsOfFile: path)
let newButton = UIButton.buttonWithType(UIButtonType.System) as UIButton newButton.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(newButton)
let newButtonConstraintL = NSLayoutConstraint(item: newButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)
let newButtonConstraintH = NSLayoutConstraint(item: newButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)
// Read from Plist the coordinate
var reloadCoordX:[String] = data!.valueForKey("\(strDaPassare) X") as[String]
var reloadCoordY:[String] = data!.valueForKey("\(strDaPassare) Y") as[String]
var coordX: Int = ("\(reloadCoordX[index-1])").toInt()!
var coordY: Int = ("\(reloadCoordY[index-1])").toInt()!
// Add Constraint
newButtonConstraintX = NSLayoutConstraint(item: newButton, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: CGFloat(coordX))
newButtonConstraintY = NSLayoutConstraint(item: newButton, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: CGFloat(coordY))
}
And it's ok !!!! but this takes as a reference center of the UIView it self for the values ​​of newButtonConstraintX and newButtonConstraintY . but if I want to set attribute for newButtonConstraintX and newButtonConstraintY for the Top left corner ?