Swift UIWebView constraints full screen - swift

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.

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])

When I am editing textfield textField(_:shouldChangeCharactersInRange:replacementString:) not get called

I am creating custom keyboard for ipad in which when I press my keyboard button and edit textfield textField(_:shouldChangeCharactersInRange:replacementString:) doesn't get called
import UIKit
class KeyBoardView: UIView{
enum KeyBoardType {
case AlphaNumeric
case Alpha
case Numeric
}
var textField: CustomKeyboardTextField!
var textInput: UITextInput?{
get{
return self.textField
}
}
var keyboardView: KeyBoardView!
let win: UIWindow = (((UIApplication.sharedApplication().delegate?.window)!)!)
let alphaKeyBoardView = KeyBoardAlphaView.instanceFromNib()
let numKeyBoardView = KeyBoardNumericView.instanceFromNib()
class func instanceFromNib() -> KeyBoardView{
let win: UIWindow = (((UIApplication.sharedApplication().delegate?.window)!)!)
let arrView = win.subviews
for view in arrView {
if view.isKindOfClass(KeyBoardView) {
view.removeFromSuperview()
break
}
}
return KeyBoardView(frame: CGRect(x: 0, y: win.bounds.size.height*0.6, width: win.bounds.size.width, height: win.bounds.size.height*0.4))
}
func keyboardTypeSet(notification: AnyObject) {
keyboardView = notification.object as! KeyBoardView
self.textField = keyboardView.textField
let arrView = win.subviews
var isPresentKView: Bool = false
for view in arrView {
if view.isKindOfClass(KeyBoardView) {
let arrSubView = view.subviews
for subView in arrSubView {
subView.removeFromSuperview()
}
isPresentKView = true
setkType(textField.kType)
break
}
}
if !isPresentKView {
setkType(textField.kType)
keyboardView.backgroundColor = UIColor.clearColor()
// setkType(textField.kType)
keyboardView.translatesAutoresizingMaskIntoConstraints = false
win.addSubview(keyboardView)
let leadingConstraint = NSLayoutConstraint(item: keyboardView, attribute: .Leading, relatedBy: .Equal, toItem: win, attribute: .Leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: keyboardView, attribute: .Trailing, relatedBy: .Equal, toItem: win, attribute: .Trailing, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: keyboardView, attribute: .Bottom, relatedBy: .Equal, toItem: win, attribute: .Bottom, multiplier: 1, constant: 0)
let heightConstraint = NSLayoutConstraint(item: keyboardView, attribute: .Height, relatedBy: .Equal, toItem: win, attribute: .Height, multiplier: 0.4, constant: 0)
win.addConstraints([leadingConstraint,trailingConstraint,bottomConstraint,heightConstraint])
keyboardView.layoutIfNeeded()
win.layoutIfNeeded()
}
}
func setkType(keyboardType: KeyBoardType) {
switch keyboardType {
case .AlphaNumeric:
alphaNumericKeyboard()
case .Alpha:
alphaKeyboard()
case .Numeric:
numericKeyboard()
}
}
func alphaNumericKeyboard() {
keyboardView.addSubview(alphaKeyBoardView)
keyboardView.addSubview(numKeyBoardView)
alphaKeyBoardView.translatesAutoresizingMaskIntoConstraints = false
numKeyBoardView.translatesAutoresizingMaskIntoConstraints = false
let alLeadingConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Leading, relatedBy: .Equal, toItem: keyboardView, attribute: .Leading, multiplier: 1, constant: 0)
let alBottomConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Bottom, relatedBy: .Equal, toItem: keyboardView, attribute: .Bottom, multiplier: 1, constant: 0)
let alTopConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Top, relatedBy: .Equal, toItem: keyboardView, attribute: .Top, multiplier: 1, constant: 0)
let alWidthConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Width, relatedBy: .Equal, toItem: keyboardView, attribute: .Width, multiplier: 0.7, constant: 0)
// let horizantalConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Right, relatedBy: .Equal, toItem: numKeyBoardView, attribute: .Left, multiplier: 1, constant: 0)
let numTrailingConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Trailing, relatedBy: .Equal, toItem: keyboardView, attribute: .Trailing, multiplier: 1, constant: 0)
let numBottomConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Bottom, relatedBy: .Equal, toItem: keyboardView, attribute: .Bottom, multiplier: 1, constant: 0)
let numLeadingConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Leading, relatedBy: .Equal, toItem: alphaKeyBoardView, attribute: .Trailing, multiplier: 1, constant: 0)
let numHeightConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Height, relatedBy: .Equal, toItem: keyboardView, attribute: .Height, multiplier: 1, constant: 0)
keyboardView.addConstraints([alLeadingConstraint,alBottomConstraint,alTopConstraint,alWidthConstraint,numBottomConstraint,numLeadingConstraint,numTrailingConstraint,numHeightConstraint])
}
func alphaKeyboard(){
alphaKeyBoardView.translatesAutoresizingMaskIntoConstraints = false
keyboardView.addSubview(alphaKeyBoardView)
let alLeadingConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Leading, relatedBy: .Equal, toItem: keyboardView, attribute: .Leading, multiplier: 1, constant: 0)
let alTrailingConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Trailing, relatedBy: .Equal, toItem: keyboardView, attribute: .Trailing, multiplier: 1, constant: 0)
let alTopConstriant = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Top, relatedBy: .Equal, toItem: keyboardView, attribute: .Top, multiplier: 1, constant: 0)
let alBottomConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Bottom, relatedBy: .Equal, toItem: keyboardView, attribute: .Bottom, multiplier: 1, constant: 0)
keyboardView.addConstraints([alLeadingConstraint,alTrailingConstraint,alTopConstriant,alBottomConstraint])
}
func numericKeyboard(){
numKeyBoardView.translatesAutoresizingMaskIntoConstraints = false
// keyboardView.frame = CGRect(x: 0, y: window!.bounds.size.height*0.6, width: window!.bounds.size.width*0.3, height: window!.bounds.height*0.4)
keyboardView.addSubview(numKeyBoardView)
let numLeadingConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Leading, relatedBy: .Equal, toItem: keyboardView, attribute: .Leading, multiplier: 1, constant: 0)
let numTopConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Top, relatedBy: .Equal, toItem: keyboardView, attribute: .Top, multiplier: 1, constant: 0)
let numWidthConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Width, relatedBy: .Equal, toItem: keyboardView, attribute: .Width, multiplier: 0.3, constant: 0)
let numBottomConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Bottom, relatedBy: .Equal, toItem: keyboardView, attribute: .Bottom, multiplier: 1, constant: 0)
// let numHeightConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Height, relatedBy: .Equal, toItem: keyboardView, attribute: .Height, multiplier: 1, constant: 0)
keyboardView.addConstraints([numLeadingConstraint,numTopConstraint,numBottomConstraint,numWidthConstraint])
}
}
Add
textField.delegate = self
Add UITextFieldDelegate to class
class KeyBoardView: UIView,UITextFieldDelegate {

How to align two uibuttons in storyboard

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)

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.