After adding an NSLayoutConstraint, view disappears - swift

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

Related

UIScrollView with NSLayoutConstraint

UIScrollView is not scrolling the subviews but it does show a scroll-bar.
Here is what I am trying to do
class SetupViewController: UIViewController {
let scrollView = UIScrollView()
let pageLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
self.setupViews()
self.setupConstraints()
self.setText()
}
func setupViews() {
self.scrollView.backgroundColor = .red
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
// Page Label
self.pageLabel.font = UIFontLocalized(englishFontSize: 22, arabicFontSize: 22)
self.pageLabel.textAlignment = .center
self.pageLabel.translatesAutoresizingMaskIntoConstraints = false
}
func setupConstraints() {
// Add To Sub Views
self.view.addSubview(self.pageLabel)
self.view.addSubview(self.scrollView)
// Page Label
NSLayoutConstraint(item: self.pageLabel, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 30.0).isActive = true
NSLayoutConstraint(item: self.pageLabel, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 20.0).isActive = true
NSLayoutConstraint(item: self.pageLabel, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: -40.0).isActive = true
NSLayoutConstraint(item: self.pageLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36.0).isActive = true
// Scroll View
NSLayoutConstraint(item: self.scrollView, attribute: .top, relatedBy: .equal, toItem: self.line1, attribute: .bottom, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: self.scrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: self.scrollView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: self.scrollView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1.0, constant: -100.0).isActive = true
self.scrollView.contentSize.height = 2000
// NSLayoutConstraint for rest of elements are removed in this example code.
}
}
I can confirm UIScrollView is added in the view because I checked by giving background red color to scrollview and it does show the background red color in correct position. My issue is the subviews does not move only the scroll bar is moving.
What could be the possible issue here?
Note: NSLayoutConstraint exist for all UIKit elements, I have not added it in the code.
I got this issue sorted our. As pointed out in the comment, the issue was related to superview in constraint which was set to self.view changing it to self.scrollView solved the issue.

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)

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 ?