Custom keyboard for iOS - ios

I've built keyboard this way. (code below)
But, there are some problems like:
when you click on some button, it's click animation taking long to get back.
there is no way to place some common keys as globe symbol for language changing or caps lock
What I want to do is, to modify original iOS keyboard and add some other buttons.
Is it possible? Any suggestions?
import UIKit
class KeyboardViewController: UIInputViewController {
#IBOutlet var nextKeyboardButton: UIButton!
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
super.viewDidLoad()
let buttonTitles1 = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"]
let buttonTitles2 = ["A", "S", "D", "F", "G", "H", "J", "K", "L"]
let buttonTitles3 = ["CP", "Z", "X", "C", "V", "B", "N", "M", "BP"]
let buttonTitles4 = ["CHG", "SPACE", "RETURN"]
var row1 = createRowOfButtons(buttonTitles1)
var row2 = createRowOfButtons(buttonTitles2)
var row3 = createRowOfButtons(buttonTitles3)
var row4 = createRowOfButtons(buttonTitles4)
self.view.addSubview(row1)
self.view.addSubview(row2)
self.view.addSubview(row3)
self.view.addSubview(row4)
row1.setTranslatesAutoresizingMaskIntoConstraints(false)
row2.setTranslatesAutoresizingMaskIntoConstraints(false)
row3.setTranslatesAutoresizingMaskIntoConstraints(false)
row4.setTranslatesAutoresizingMaskIntoConstraints(false)
addConstraintsToInputView(self.view, rowViews: [row1, row2, row3, row4])
}
func createRowOfButtons(buttonTitles: [NSString]) -> UIView {
var buttons = [UIButton]()
var keyboardRowView = UIView(frame: CGRectMake(0, 0, 320, 50))
for buttonTitle in buttonTitles{
let button = createButtonWithTitle(buttonTitle)
buttons.append(button)
keyboardRowView.addSubview(button)
}
addIndividualButtonConstraints(buttons, mainView: keyboardRowView)
return keyboardRowView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(textInput: UITextInput) {
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(textInput: UITextInput) {
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
var proxy = self.textDocumentProxy as UITextDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
textColor = UIColor.whiteColor()
} else {
textColor = UIColor.blackColor()
}
}
func createButtonWithTitle(title: String) -> UIButton {
let button = UIButton.buttonWithType(.System) as UIButton
button.frame = CGRectMake(0, 0, 20, 20)
button.setTitle(title, forState: .Normal)
button.sizeToFit()
button.titleLabel?.font = UIFont.systemFontOfSize(15)
button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.backgroundColor = UIColor(white: 1.0, alpha: 1.0)
button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
button.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)
return button
}
func didTapButton(sender: AnyObject?) {
let button = sender as UIButton
var proxy = textDocumentProxy as UITextDocumentProxy
if let title = button.titleForState(.Normal) {
switch title {
case "BP" :
proxy.deleteBackward()
case "RETURN" :
proxy.insertText("\n")
case "SPACE" :
proxy.insertText(" ")
case "CHG" :
self.advanceToNextInputMode()
default :
proxy.insertText(title)
}
}
}
func addIndividualButtonConstraints(buttons: [UIButton], mainView: UIView){
for (index, button) in enumerate(buttons) {
var topConstraint = NSLayoutConstraint(item: button, attribute: .Top, relatedBy: .Equal, toItem: mainView, attribute: .Top, multiplier: 1.0, constant: 1)
var bottomConstraint = NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .Equal, toItem: mainView, attribute: .Bottom, multiplier: 1.0, constant: -1)
var rightConstraint : NSLayoutConstraint!
if index == buttons.count - 1 {
rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .Equal, toItem: mainView, attribute: .Right, multiplier: 1.0, constant: -1)
}else{
let nextButton = buttons[index+1]
rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .Equal, toItem: nextButton, attribute: .Left, multiplier: 1.0, constant: -1)
}
var leftConstraint : NSLayoutConstraint!
if index == 0 {
leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .Equal, toItem: mainView, attribute: .Left, multiplier: 1.0, constant: 1)
}else{
let prevtButton = buttons[index-1]
leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .Equal, toItem: prevtButton, attribute: .Right, multiplier: 1.0, constant: 1)
let firstButton = buttons[0]
var widthConstraint = NSLayoutConstraint(item: firstButton, attribute: .Width, relatedBy: .Equal, toItem: button, attribute: .Width, multiplier: 1.0, constant: 0)
widthConstraint.priority = 800
mainView.addConstraint(widthConstraint)
}
mainView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
}
}
func addConstraintsToInputView(inputView: UIView, rowViews: [UIView]){
for (index, rowView) in enumerate(rowViews) {
var rightSideConstraint = NSLayoutConstraint(item: rowView, attribute: .Right, relatedBy: .Equal, toItem: inputView, attribute: .Right, multiplier: 1.0, constant: -1)
var leftConstraint = NSLayoutConstraint(item: rowView, attribute: .Left, relatedBy: .Equal, toItem: inputView, attribute: .Left, multiplier: 1.0, constant: 1)
inputView.addConstraints([leftConstraint, rightSideConstraint])
var topConstraint: NSLayoutConstraint
if index == 0 {
topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: inputView, attribute: .Top, multiplier: 1.0, constant: 0)
}else{
let prevRow = rowViews[index-1]
topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: prevRow, attribute: .Bottom, multiplier: 1.0, constant: 0)
let firstRow = rowViews[0]
var heightConstraint = NSLayoutConstraint(item: firstRow, attribute: .Height, relatedBy: .Equal, toItem: rowView, attribute: .Height, multiplier: 1.0, constant: 0)
heightConstraint.priority = 800
inputView.addConstraint(heightConstraint)
}
inputView.addConstraint(topConstraint)
var bottomConstraint: NSLayoutConstraint
if index == rowViews.count - 1 {
bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: inputView, attribute: .Bottom, multiplier: 1.0, constant: 0)
}else{
let nextRow = rowViews[index+1]
bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: nextRow, attribute: .Top, multiplier: 1.0, constant: 0)
}
inputView.addConstraint(bottomConstraint)
}
}
}

Use the github repo below. It's nearly 1:1 immitation of iOS 8 original keyboard and works like a charm
https://github.com/archagon/tasty-imitation-keyboard

when you click on some button, it's click animation taking long to get back.
Your keys are nothing more than UIButtons. The fade-out animation you're seeing is the normal animation for a UIButton—you can see it for yourself on most buttons across the system. Instead of initializing it with UIButtonType.System, use .Custom and set your own appearance for the desired states.
As an example (there are many possibilities):
let button = UIButton(.Custom)
button.setTitleColor(UIColor.redColor(), forState: .Highlighted)
Note that you don't even have to restrict yourself to using UIButton—custom keyboards are a blank slate.
there is no way to place some common keys as globe symbol for language changing or caps lock
From the Custom Keyboard Guide:
The system picks the appropriate “next” keyboard; there is no API to
obtain a list of enabled keyboards or for picking a particular
keyboard to switch to.
So these "special" keys are also up to you to provide, most likely with your own custom icons. If you're using UIButton, that probably means calling setImage:forState:. It looks like most third-party keyboards use a globe icon almost identicial to the system one for the "next" key.
There is no API to mod the system keyboard—you must build one yourself from the ground-up.

For slow animation, you can study these github repo on how they do their animation:
https://github.com/YuAo/WUEmoticonsKeyboard (See the popview when the button gets pressed.)
https://github.com/ayushgoel/AGEmojiKeyboard
https://github.com/kulpreetchilana/Custom-iOS-Keyboards
or search github:https://github.com/search?utf8=✓&q=iOS+keyboard
Note: They are mostly objective-c repo.
Lastly, it might be cheaper to install a custom keyboard than to make one:
http://www.imore.com/best-custom-keyboards-ios-8

Related

Swift: Custom Navigationbar with centered buttons

I have written a custom navigationbar but habe the problem, that the text inside my buttons is not perfectly aligned. For example see the + Button.
Any ideas how to improve this?
import UIKit
class DashboardNavbar: UINavigationBar {
var titleLabel: UILabel?
private var plusButton: UIButton?
private var editButton: UIButton?
var tap: UIGestureRecognizer?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
isTranslucent = false
addTitleLabel()
addPlusButton()
addEditButton()
}
private func addTitleLabel() {
titleLabel = UILabel()
titleLabel?.textColor = Styling.navigationbarTintColor
titleLabel?.translatesAutoresizingMaskIntoConstraints = false
titleLabel?.font = UIFont(name: "DIN Alternate", size: 17)
if let titleLabel = titleLabel{
addSubview(titleLabel)
let leadingConstraint = NSLayoutConstraint(item: titleLabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 15)
addConstraint(leadingConstraint)
let centerYConstraint = NSLayoutConstraint(item: titleLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0)
addConstraint(centerYConstraint)
}
}
private func addPlusButton(){
plusButton = UIButton(type: .custom)
plusButton?.setTitle("+", for: .normal)
plusButton?.layer.backgroundColor = Styling.navigationbarTintColor.cgColor
plusButton?.layer.cornerRadius = 15
plusButton?.setTitleColor(.black, for: .normal)
plusButton?.titleLabel?.font = UIFont(name: "DIN Alternate", size: 20)
plusButton?.contentVerticalAlignment = .center
plusButton?.contentHorizontalAlignment = .center
plusButton?.titleLabel?.baselineAdjustment = .alignCenters
if let plusButton = plusButton{
addSubview(plusButton)
plusButton.translatesAutoresizingMaskIntoConstraints = false
let trailingConstraint = NSLayoutConstraint(item: plusButton, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: -15)
addConstraint(trailingConstraint)
let centerYConstraint = NSLayoutConstraint(item: plusButton, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0)
addConstraint(centerYConstraint)
let widthC = NSLayoutConstraint(item: plusButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
addConstraint(widthC)
let heightC = NSLayoutConstraint(item: plusButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
addConstraint(heightC)
}
}
private func addEditButton(){
editButton = UIButton(type: .custom)
editButton?.setTitle(LocalizedString("EDIT"), for: .normal)
editButton?.layer.backgroundColor = Styling.navigationbarTintColor.cgColor
editButton?.layer.cornerRadius = 15
editButton?.setTitleColor(.black, for: .normal)
editButton?.titleLabel?.font = UIFont(name: "DIN Alternate", size: 16)
editButton?.contentVerticalAlignment = .center
editButton?.contentHorizontalAlignment = .center
editButton?.titleLabel?.baselineAdjustment = .alignCenters
if let editButton = editButton{
addSubview(editButton)
editButton.translatesAutoresizingMaskIntoConstraints = false
let trailingConstraint = NSLayoutConstraint(item: editButton, attribute: .trailing, relatedBy: .equal, toItem: plusButton!, attribute: .leading, multiplier: 1.0, constant: -15)
addConstraint(trailingConstraint)
let centerYConstraint = NSLayoutConstraint(item: editButton, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0)
addConstraint(centerYConstraint)
let widthC = NSLayoutConstraint(item: editButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 80)
addConstraint(widthC)
let heightC = NSLayoutConstraint(item: editButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
addConstraint(heightC)
}
}
func setTitle(title: String){
titleLabel?.text = title
}
}
There several ways to correct this:
Use + image, image will be centered correctly on UIButton.
Change title insets of the UIButton, give some more inset from bottom and left, UIButton has titleInsets property;
Change the font o_O which is silly idea but also work, you can edit the font and make "+" sign to be centered on the drawing area.
Hope this helps! Good luck!

How to update constraints dynamically?

I have three UIButton, I have created programmatically constraints, at some condition i am removing one of UIButton as button.removeFromSuperview() & rest of two button will set constraints as per priorities.
Issue is when i am removing one UIButton(buttonWink) then as of viewLifeCycle viewWillLayoutSubviews will called & App crashes in below line
buttonWink.translatesAutoresizingMaskIntoConstraints = false
Ofcourse because buttonWink is removed from superview however we can check before setting constraints as
if buttonWink != nil {
buttonWink.translatesAutoresizingMaskIntoConstraints = false
}
But by checking nil for every button will make code lengthy, is there any way of doing the same? i will really appreciate friends.
Output -
Here i am attaching my tried full code.
import UIKit
class MasterViewController: UIViewController {
#IBOutlet weak var buttonMessage : UIButton!
#IBOutlet weak var buttonLike : UIButton!
#IBOutlet weak var buttonWink : UIButton!
#IBAction func tapsOnLike(_ sender: UIButton) {
sender.removeFromSuperview()
}
#IBAction func tapsOnWink(_ sender: UIButton) {
sender.removeFromSuperview()
}
#IBAction func tapsOnNextButton(){
let vc = DetailViewController(nibName: "DetailViewController", bundle: nil)
navigationController?.pushViewController(vc, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
setConstraints()
navigationController?.isNavigationBarHidden = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
// setConstraints()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
//setConstraints()
}
func setConstraints() {
if buttonMessage != nil {
buttonMessage?.translatesAutoresizingMaskIntoConstraints = false
}
buttonLike?.translatesAutoresizingMaskIntoConstraints = false
buttonWink?.translatesAutoresizingMaskIntoConstraints = false
//Constraints for Message button
let leading = NSLayoutConstraint(item: buttonMessage, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 8)
leading.isActive = true
let trailingToSuperView = NSLayoutConstraint(item: buttonMessage, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -8)
trailingToSuperView.priority = 998
trailingToSuperView.isActive = true
let bottomToSuperView = NSLayoutConstraint(item: buttonMessage, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: -8)
bottomToSuperView.isActive = true
let trailingToWink = NSLayoutConstraint(item: buttonMessage, attribute: .trailing, relatedBy: .equal, toItem: buttonWink, attribute: .leading, multiplier: 1, constant: -8)
trailingToWink.priority = 999
trailingToWink.isActive = true
let leadingToLike = NSLayoutConstraint(item: buttonMessage, attribute: .trailing, relatedBy: .equal, toItem: buttonLike, attribute: .leading, multiplier: 1.0, constant: -8)
leadingToLike.isActive = true
let alignBottomToWink = NSLayoutConstraint(item: buttonMessage, attribute: .bottom, relatedBy: .equal, toItem: buttonWink, attribute: .bottom, multiplier: 1, constant: 0)
alignBottomToWink.isActive = true
let alignBottomToLike = NSLayoutConstraint(item: buttonMessage, attribute: .bottom, relatedBy: .equal, toItem: buttonLike, attribute: .bottom, multiplier: 1, constant: 0)
alignBottomToLike.isActive = true
let equalWidthToWink = NSLayoutConstraint(item: buttonMessage, attribute: .width, relatedBy: .equal, toItem: buttonWink, attribute: .width, multiplier: 1, constant: 0)
equalWidthToWink.isActive = true
let equalWidthToLike = NSLayoutConstraint(item: buttonMessage, attribute: .width, relatedBy: .equal, toItem: buttonLike, attribute: .width, multiplier: 1, constant: 0)
equalWidthToLike.isActive = true
//Constraints for like button
let trailingLikeToSuperView = NSLayoutConstraint(item: buttonLike, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -8)
trailingLikeToSuperView.priority = 999
trailingLikeToSuperView.isActive = true
let leadingToWink = NSLayoutConstraint(item: buttonLike, attribute: .trailing, relatedBy: .equal, toItem: buttonWink, attribute: .leading, multiplier: 1.0, constant: -8)
leadingToWink.isActive = true
//Constraints for Wink button
let trailingWinkToSuperView = NSLayoutConstraint(item: buttonWink, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -8)
trailingWinkToSuperView.isActive = true
}
}
Just FYI -
simply use a stack view for this.
It is incredibly easy: it's why Apple finally added a stack concept a couple yrs ago.
I just added UIStackView & it's too easy,
Three buttons are added in horizontal stack view with 8 points spacing & added three constraints to stack view as leading, trailing & bottom to superview
#IBOutlet weak var stackView: UIStackView!
#IBAction func tapsOnLikeInStack(_ sender: UIButton) {
sender.isHidden = true
}
#IBAction func tapsOnWinkInStack(_ sender: UIButton) {
sender.isHidden = true
}
func constraintsForStackView(){
stackView?.translatesAutoresizingMaskIntoConstraints = false
stackView.spacing = 8
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.alignment = .fill
let leading = NSLayoutConstraint(item: stackView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 8)
leading.isActive = true
let trailingToSuperView = NSLayoutConstraint(item: stackView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -8)
trailingToSuperView.isActive = true
let bottomToSuperView = NSLayoutConstraint(item: stackView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: -120)
bottomToSuperView.isActive = true
}
as far as your viewWillLayoutSubviews code put your buttons into an array to use:
for btn in buttons {
if (btn.superview != nil) {
btn.translatesAutoresizingMaskIntoConstraints = false
}
}
and if you want to change the constraints for the buttons still in the view, give them identifiers when you create the constraints:
for btn in buttons {
if btn.superview != nil {
btn.translatesAutoresizingMaskIntoConstraints = false
for constraint in btn.constraints {
switch constraint.identifier {
case "winkBtnTrailing":
//do stuff like constraint = new layout constraint
//or constraint.firstItem = *new first Item to base trailing on*
break
default:
//don't do stuff?
break
}
}
}
}
Just as a FYI using button.removeFromSuperview() doesn't set that button to nil, it just removes it from the view you called .addSubview(button) on

Getting frame of programmatically created subviews

I have a programmatically created subviews, view1. I am adding some UIButtons to it, but the measurements of these buttons depend on view1.frame.height. However, I can only seem to get the value in viewDidAppear, which obviously is not what I want. Moreover, if I put my addButtons() function in viewDidLayoutSubviews, I get an infinite loop. The results of putting addButtons() in various UIViewController functions is as follows (in comments):
override func viewDidLoad() {
super.viewDidLoad()
addView1()
}
override func viewDidLayoutSubviews(){
print(view1.frame.height) // 0.0 first time it is called, 74.0, which is right, the second time it is called, but I got into an infinite loop
//addButtons() -> infinite loop
}
override func viewWillAppear(animated: Bool) {
print(view1.frame.height) //0.0
}
override func viewDidAppear(animated: Bool) {
print(view1.frame.height) //74.0, the value I want, but obviously not where I want it
}
My addButtons() looks like this:
func addButtons(){
let mon = UIButton()
let tue = UIButton()
let wed = UIButton()
let thu = UIButton()
let fri = UIButton()
let sat = UIButton()
let sun = UIButton()
let buttonsArray = ["mon": mon, "tue": tue, "wed": wed, "thu": thu, "fri": fri, "sat": sat, "sun": sun]
let daysArray1 = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
var daysArray2 = ["mon": "M", "tue":"T", "wed": "W", "thu": "T", "fri": "F", "sat": "S", "sun": "S"]
let buttonSide = view1.frame.height * 0.6
let distance = (view1.frame.width - buttonSide * 7) / 8.0
var count = 0
for day in daysArray1{
let offSet = (buttonSide ) * CGFloat(count) + distance * CGFloat(count + 1)
let centerX = (buttonSide / 2) + offSet
buttonsArray[day]!.setTitle(daysArray2[day], forState: .Normal)
buttonsArray[day]!.translatesAutoresizingMaskIntoConstraints = false
buttonsArray[day]!.layer.cornerRadius = 5
view1.addSubview(buttonsArray[day]!)
view1.addConstraint(NSLayoutConstraint(item: buttonsArray[day]!, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: view1, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
view1.addConstraint(NSLayoutConstraint(item: buttonsArray[day]!, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: view1, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: centerX))
view1.addConstraint(NSLayoutConstraint(item: buttonsArray[day]!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: buttonSide))
view1.addConstraint(NSLayoutConstraint(item: buttonsArray[day]!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: buttonSide))
count += 1
}
Every time you add a button to your view the system calls viewDidLayoutSubviews which you keep adding buttons and the system keeps calling viewDidLayoutSubviews. You should add your buttons when viewDidLoad().
To match the height of the view you can:
Pin all buttons to Trailing and Leading edge of the view
Pin the first button's Top with view's Top Edge
Pin the last button's Bottom with view's Bottom
Pin each button's Top with the previous button's Bottom edge (given is not the first button)
Constraint all buttons .Height to match view's .Height with a multiplier of 0.6
This is an example I did using PureLayout
let someView = UIView(frame: CGRectZero)
view.addSubview(someView)
var prev: UIButton?
for i in 0..<6 {
let button = UIButton(frame: CGRectZero)
button.setBackgroundColor(UIColor.randomColor(), forUIControlState: .Normal)
someView.addSubview(button)
button.autoPinEdgeToSuperviewEdge(.Trailing)
button.autoPinEdgeToSuperviewEdge(.Leading)
button.autoMatchDimension(.Height, toDimension: .Height, ofView: someView, withMultiplier: 0.6)
if let previous = prev {
button.autoPinEdge(.Top, toEdge: .Bottom, ofView: previous)
} else {
button.autoPinEdgeToSuperviewEdge(.Top)
}
prev = button
}
someView.autoPinEdgeToSuperviewEdge(.Trailing)
someView.autoPinEdgeToSuperviewEdge(.Leading)
someView.autoPinEdgeToSuperviewEdge(.Top)
someView.autoSetDimension(.Height, toSize: 100)
UPDATE(equal horizontal spacing)
OK based your comment, I got that you also need equal spacing between buttons as well as the side edges of the parent container. where is a full working view controller class:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let wrapper = UIView(frame: CGRectZero)
view.addSubview(wrapper)
wrapper.backgroundColor = UIColor.redColor()
var previous: (button: UIButton, spacer: UIView)?
for day in ["M", "T", "W", "T", "F", "S", "S"] {
let button = UIButton(frame: CGRectZero)
let spacer = UIView(frame: CGRectZero)
button.setTitle(day, forState: .Normal)
button.backgroundColor = UIColor.grayColor()
button.translatesAutoresizingMaskIntoConstraints = false
spacer.translatesAutoresizingMaskIntoConstraints = false
wrapper.addSubview(button)
wrapper.addSubview(spacer)
//set height 60% of wrapper
wrapper.addConstraint(NSLayoutConstraint(
item: button,
attribute: .Height,
relatedBy: .Equal,
toItem: wrapper,
attribute: .Height,
multiplier: 0.6,
constant: 0))
//set width same has height
wrapper.addConstraint(NSLayoutConstraint(
item: button,
attribute: .Height,
relatedBy: .Equal,
toItem: button,
attribute: .Width,
multiplier: 1,
constant: 0))
//pin button leading with spacer trailing
wrapper.addConstraint(NSLayoutConstraint(
item: button,
attribute: .Leading,
relatedBy: .Equal,
toItem: spacer,
attribute: .Trailing,
multiplier: 1,
constant: 0))
//pin spacer trailing with button leading
wrapper.addConstraint(NSLayoutConstraint(
item: spacer,
attribute: .Trailing,
relatedBy: .Equal,
toItem: button,
attribute: .Leading,
multiplier: 1,
constant: 0))
//align button to center
wrapper.addConstraint(NSLayoutConstraint(
item: button,
attribute: .CenterY,
relatedBy: .Equal,
toItem: wrapper,
attribute: .CenterY,
multiplier: 1,
constant: 0))
if let previous = previous {
//pin spacer Leading with previous button trailing
wrapper.addConstraint(NSLayoutConstraint(
item: spacer,
attribute: .Leading,
relatedBy: .Equal,
toItem: previous.button,
attribute: .Trailing,
multiplier: 1,
constant: 0))
//pin previous button's Trailing with spacer Leading
wrapper.addConstraint(NSLayoutConstraint(
item: previous.button,
attribute: .Trailing,
relatedBy: .Equal,
toItem: spacer,
attribute: .Leading,
multiplier: 1,
constant: 0))
//set spacer's width same as previous spacer
wrapper.addConstraint(NSLayoutConstraint(
item: spacer,
attribute: .Width,
relatedBy: .Equal,
toItem: previous.spacer,
attribute: .Width,
multiplier: 1,
constant: 0))
} else {
//this is the first item, pin the spacer Leading to wrapper Leading
wrapper.addConstraint(NSLayoutConstraint(
item: spacer,
attribute: .Leading,
relatedBy: .Equal,
toItem: wrapper,
attribute: .Leading,
multiplier: 1,
constant: 0))
}
previous = (button: button, spacer: spacer)
}
// last spacer
if let previous = previous {
let spacer = UIView(frame: CGRectZero)
spacer.translatesAutoresizingMaskIntoConstraints = false
wrapper.addSubview(spacer)
//pin spacer Leading with previous button trailing
wrapper.addConstraint(NSLayoutConstraint(
item: spacer,
attribute: .Leading,
relatedBy: .Equal,
toItem: previous.button,
attribute: .Trailing,
multiplier: 1,
constant: 0))
//pin previous button's Trailing with spacer Leading
wrapper.addConstraint(NSLayoutConstraint(
item: previous.button,
attribute: .Trailing,
relatedBy: .Equal,
toItem: spacer,
attribute: .Leading,
multiplier: 1,
constant: 0))
//set spacer's width same as previous spacer
wrapper.addConstraint(NSLayoutConstraint(
item: spacer,
attribute: .Width,
relatedBy: .Equal,
toItem: previous.spacer,
attribute: .Width,
multiplier: 1,
constant: 0))
//pin spacer's Trailing with wrappper Trailing
wrapper.addConstraint(NSLayoutConstraint(
item: spacer,
attribute: .Trailing,
relatedBy: .Equal,
toItem: wrapper,
attribute: .Trailing,
multiplier: 1,
constant: 0))
}
wrapper.frame = CGRect(x: 0, y: 100, width: 320, height: 40)
}
}

How can I make the type emoji or type anything

I am currently working on a custom keyboard
and I encountered a problem when I try to combine the textDocumentProxy in a new window
So have add emoji keyboard button
  and which I pass page with the addition of two buttons of emoji Now my problem is how I can incorporate textDocumentProxy another window or link between them waned even can type emoji
I try to type anything not only emoji
Full source code is here
https://github.com/archagon/tasty-imitation-keyboard
import UIKit
class DefaultSettings1: ExtraView, UITableViewDelegate {
#IBOutlet var tableView1: UITableView?
#IBOutlet var effectsView: UIVisualEffectView?
#IBOutlet var backButton1: UIButton?
#IBOutlet var settingsLabel: UILabel?
#IBOutlet var pixelLine: UIView?
#IBOutlet var keyPressed: UIButton?
override var darkMode: Bool {
didSet {
self.updateAppearance(darkMode)
}
}
let cellBackgroundColorDark = UIColor.whiteColor().colorWithAlphaComponent(CGFloat(0.25))
let cellBackgroundColorLight = UIColor.whiteColor().colorWithAlphaComponent(CGFloat(1))
let cellLabelColorDark = UIColor.whiteColor()
let cellLabelColorLight = UIColor.blackColor()
let cellLongLabelColorDark = UIColor.lightGrayColor()
let cellLongLabelColorLight = UIColor.grayColor()
// TODO: these probably don't belong here, and also need to be localized
required init(globalColors: GlobalColors.Type?, darkMode: Bool, solidColorMode: Bool) {
super.init(globalColors: globalColors, darkMode: darkMode, solidColorMode: solidColorMode)
self.loadNib()
}
required init(coder aDecoder: NSCoder) {
fatalError("loading from nib not supported")
}
func loadNib() {
let assets = NSBundle(forClass: self.dynamicType).loadNibNamed("DefaultSettings1", owner: self, options: nil)
if assets.count > 0 {
if var rootView = assets.first as? UIView {
rootView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.addSubview(rootView)
let left = NSLayoutConstraint(item: rootView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0)
let right = NSLayoutConstraint(item: rootView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)
let top = NSLayoutConstraint(item: rootView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: rootView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
self.addConstraint(left)
self.addConstraint(right)
self.addConstraint(top)
self.addConstraint(bottom)
}
}
self.tableView1?.registerClass(DefaultSettings1TableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView1?.estimatedRowHeight = 44;
self.tableView1?.rowHeight = UITableViewAutomaticDimension;
// XXX: this is here b/c a totally transparent background does not support scrolling in blank areas
self.tableView1?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.01)
self.updateAppearance(self.darkMode)
}
func tableView1(tableView1: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if var cell = tableView1.dequeueReusableCellWithIdentifier("cell") as? DefaultSettings1TableViewCell {
if cell.sw.allTargets().count == 0 {
cell.sw.addTarget(self, action: Selector("toggleSetting1:"), forControlEvents: UIControlEvents.ValueChanged)
}
cell.backgroundColor = (self.darkMode ? cellBackgroundColorDark : cellBackgroundColorLight)
cell.label.textColor = (self.darkMode ? cellLabelColorDark : cellLabelColorLight)
cell.longLabel.textColor = (self.darkMode ? cellLongLabelColorDark : cellLongLabelColorLight)
cell.changeConstraints()
return cell
}
else {
assert(false, "this is a bad thing that just happened")
return UITableViewCell()
}
}
func updateAppearance(dark: Bool) {
if dark {
self.effectsView?.effect
let blueColor = UIColor(red: 135/CGFloat(255), green: 206/CGFloat(255), blue: 250/CGFloat(255), alpha: 1)
self.pixelLine?.backgroundColor = blueColor.colorWithAlphaComponent(CGFloat(0.5))
self.backButton1?.setTitleColor(blueColor, forState: UIControlState.Normal)
self.settingsLabel?.textColor = UIColor.whiteColor()
if let visibleCells = self.tableView1?.visibleCells() {
for cell in visibleCells {
if var cell = cell as? UITableViewCell {
cell.backgroundColor = cellBackgroundColorDark
var label = cell.viewWithTag(2) as? UILabel
label?.textColor = cellLabelColorDark
var longLabel = cell.viewWithTag(3) as? UITextView
longLabel?.textColor = cellLongLabelColorDark
}
}
}
}
else {
let blueColor = UIColor(red: 0/CGFloat(255), green: 122/CGFloat(255), blue: 255/CGFloat(255), alpha: 1)
self.pixelLine?.backgroundColor = blueColor.colorWithAlphaComponent(CGFloat(0.5))
self.backButton1?.setTitleColor(blueColor, forState: UIControlState.Normal)
self.settingsLabel?.textColor = UIColor.grayColor()
if let visibleCells = self.tableView1?.visibleCells() {
for cell in visibleCells {
if var cell = cell as? UITableViewCell {
cell.backgroundColor = cellBackgroundColorLight
var label = cell.viewWithTag(2) as? UILabel
label?.textColor = cellLabelColorLight
var longLabel = cell.viewWithTag(3) as? UITextView
longLabel?.textColor = cellLongLabelColorLight
}
}
}
}
}
}
class DefaultSettings1TableViewCell: UITableViewCell {
var sw: UISwitch
var label: UILabel
var longLabel: UITextView
var constraintsSetForLongLabel: Bool
var cellConstraints: [NSLayoutConstraint]
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
self.sw = UISwitch()
self.label = UILabel()
self.longLabel = UITextView()
self.cellConstraints = []
self.constraintsSetForLongLabel = false
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.sw.setTranslatesAutoresizingMaskIntoConstraints(false)
self.label.setTranslatesAutoresizingMaskIntoConstraints(false)
self.longLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
self.longLabel.text = nil
self.longLabel.scrollEnabled = false
self.longLabel.selectable = false
self.longLabel.backgroundColor = UIColor.clearColor()
self.sw.tag = 1
self.label.tag = 2
self.longLabel.tag = 3
self.addSubview(self.sw)
self.addSubview(self.label)
self.addSubview(self.longLabel)
self.addConstraints()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addConstraints() {
let margin: CGFloat = 8
let sideMargin = margin * 2
let hasLongText = self.longLabel.text != nil && !self.longLabel.text.isEmpty
if hasLongText {
let switchSide = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -sideMargin)
let switchTop = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: margin)
let labelSide = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: sideMargin)
let labelCenter = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: sw, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
self.addConstraint(switchSide)
self.addConstraint(switchTop)
self.addConstraint(labelSide)
self.addConstraint(labelCenter)
let left = NSLayoutConstraint(item: longLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: sideMargin)
let right = NSLayoutConstraint(item: longLabel, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -sideMargin)
let top = NSLayoutConstraint(item: longLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: sw, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: margin)
let bottom = NSLayoutConstraint(item: longLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -margin)
self.addConstraint(left)
self.addConstraint(right)
self.addConstraint(top)
self.addConstraint(bottom)
self.cellConstraints += [switchSide, switchTop, labelSide, labelCenter, left, right, top, bottom]
self.constraintsSetForLongLabel = true
}
else {
let switchSide = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -sideMargin)
let switchTop = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: margin)
let switchBottom = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -margin)
let labelSide = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: sideMargin)
let labelCenter = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: sw, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
self.addConstraint(switchSide)
self.addConstraint(switchTop)
self.addConstraint(switchBottom)
self.addConstraint(labelSide)
self.addConstraint(labelCenter)
self.cellConstraints += [switchSide, switchTop, switchBottom, labelSide, labelCenter]
self.constraintsSetForLongLabel = false
}
}
// XXX: not in updateConstraints because it doesn't play nice with UITableViewAutomaticDimension for some reason
func changeConstraints() {
let hasLongText = self.longLabel.text != nil && !self.longLabel.text.isEmpty
if hasLongText != self.constraintsSetForLongLabel {
self.removeConstraints(self.cellConstraints)
self.cellConstraints.removeAll()
self.addConstraints()
}
}
}
You need to change many things into your project as per your requirement like you can use that setting view by hiding that tableView and add one UIView on it after that you can add emojis into that view and when user click on setting button from keyboard you can hide your emojis view and show tableview but when user want to type smiles that you don't need to hide anything just show your emojis view.
HERE is your updated project.

IOS 8 keyboard how to swap between the decimal and numeric keyboard + shift key

I am busy making an IOS 8 keyboard and I need some help.
I have made a basic keyboard and everything works fine but I have a bumps and I need some help to slove them. I wrote the code in swift(keep in mind I'm new to swift).
I need help with the following:
To swap the keyboard from decimal(the normal one with letters) to numeric(the number/symbol one) I wrote some code for it (It basically hides the keyboards respectively and shows them again) but I don't know how to make it run when I tap the "123" button.
When I type a key eg. "A" it prints out exactly how it appears on the keyboard how can I make it automatically lowercase and then implement a shift button to give me capital letters?
Some help to solve this would be much appreciated.
Here is the code I have so far:
import UIKit
class KeyboardViewController: UIInputViewController
{
#IBOutlet var nextKeyboardButton: UIButton!
var allButtons = [UIButton]()
override func updateViewConstraints()
{
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad()
{
//Normal Keyboard start
super.viewDidLoad()
let buttonTitles1 = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"]
let buttonTitles2 = ["A", "S", "D", "F", "G", "H", "J", "K", "L"]
let buttonTitles3 = ["⬆︎", "Z", "X", "C", "V", "B", "N", "M", "⬅︎"]
let buttonTitles4 = ["123","🌐","⎯", "Return"]
//⚒
var row1 = createRowOfButtons(buttonTitles1)
var row2 = createRowOfButtons(buttonTitles2)
var row3 = createRowOfButtons(buttonTitles3)
var row4 = createRowOfButtons(buttonTitles4)
self.view.addSubview(row1)
self.view.addSubview(row2)
self.view.addSubview(row3)
self.view.addSubview(row4)
row1.setTranslatesAutoresizingMaskIntoConstraints(false)
row2.setTranslatesAutoresizingMaskIntoConstraints(false)
row3.setTranslatesAutoresizingMaskIntoConstraints(false)
row4.setTranslatesAutoresizingMaskIntoConstraints(false)
addConstraintsToInputView(self.view, rowViews: [row1, row2, row3, row4])
applyCurrentTheme()
//Normal keyboard end
//Change to numeric keyboard start
let buttonsNum1FirstRow = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
let buttonsNum2FirstRow = ["-", "/", ":", ";", "(", ")", "$", "&", "#", "\""]
let buttonsNum3FirstRow = ["#+=", ".", ",", "?", "!", "'", "7", "⬅︎"]
let buttonsNum4FirstRow = ["ABC","⎯","Return"]
//define rows as class variable instead of function variable
var Srow1:UIView!
var Srow2:UIView!
var Srow3:UIView!
var Srow4:UIView!
var Srow1Num:UIView!
//Hide it like below:
func didTapChangeToNumbersButton(sender: AnyObject?)
{
Srow1.hidden = true
Srow1Num.hidden = false
}
//Change to numeric keyboard end
}
func createRowOfButtons(buttonTitles: [NSString]) -> UIView
{
var buttons = [UIButton]()
var keyboardRowView = UIView(frame: CGRectMake(0, 0, 320, 50))
for buttonTitle in buttonTitles
{
let button = createButtonWithTitle(buttonTitle)
buttons.append(button)
keyboardRowView.addSubview(button)
}
allButtons += buttons
addIndividualButtonConstraints(buttons, mainView: keyboardRowView)
return keyboardRowView
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(textInput: UITextInput)
{
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(textInput: UITextInput)
{
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
var proxy = self.textDocumentProxy as UITextDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.Dark
{
textColor = UIColor.whiteColor()
}
else
{
textColor = UIColor.blackColor()
}
}
func createButtonWithTitle(title: String) -> UIButton
{
let button = UIButton.buttonWithType(.System) as UIButton
button.frame = CGRectMake(0, 0, 20, 20)
button.setTitle(title, forState: .Normal)
button.sizeToFit()
button.titleLabel?.font = UIFont.systemFontOfSize(10)
button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.backgroundColor = UIColor(white: 1.0, alpha: 1.0)
button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
button.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)
return button
}
func didTapButton(sender: AnyObject?)
{
let button = sender as UIButton
var proxy = textDocumentProxy as UITextDocumentProxy
let capon = false;
let capslast = false;
let capslock = false;
let spacelast = false;
if let title = button.titleForState(.Normal)
{
switch title
{
case "🌐" :
self.advanceToNextInputMode()
case "." :
let capon = true
proxy.insertText(".")
let spacelast = false
let capslast = false
//Caps idea
//this caps lock code is experimental and actually doesn't work, just an idea I was playing with
case "⬆︎" :
let capon = !capon
if capslock
{
let capon = true
let capslock = false
let capslast = false
}
let capslast = true
//Caps idea
case "⬅︎" :
proxy.deleteBackward()
let spacelast = false
let capslast = false
case "Return" :
proxy.insertText("\n")
let spacelast = false
let capslast = false
case "⎯" :
if spacelast
{
proxy.deleteBackward()
proxy.insertText(". ")
let spacelast = false
let capon = true
}
else
{
proxy.insertText(" ")
let spacelast = true
}
let capslast = false
default :
proxy.insertText(title)
}
}
}
func applyCurrentTheme()
{
var theme = ClaptonTheme()
theme.applyThemeToView(self.view)
theme.applyThemeToKeys(allButtons)
}
func addIndividualButtonConstraints(buttons: [UIButton], mainView: UIView)
{
for (index, button) in enumerate(buttons)
{
var topConstraint = NSLayoutConstraint(item: button, attribute: .Top, relatedBy: .Equal, toItem: mainView, attribute: .Top, multiplier: 1.0, constant: 1)
var bottomConstraint = NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .Equal, toItem: mainView, attribute: .Bottom, multiplier: 1.0, constant: -1)
var rightConstraint : NSLayoutConstraint!
if index == buttons.count - 1
{
rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .Equal, toItem: mainView, attribute: .Right, multiplier: 1.0, constant: -1)
}
else
{
let nextButton = buttons[index+1]
rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .Equal, toItem: nextButton, attribute: .Left, multiplier: 1.0, constant: -1)
}
var leftConstraint : NSLayoutConstraint!
if index == 0 {
leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .Equal, toItem: mainView, attribute: .Left, multiplier: 1.0, constant: 1)
}
else
{
let prevtButton = buttons[index-1]
leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .Equal, toItem: prevtButton, attribute: .Right, multiplier: 1.0, constant: 1)
let firstButton = buttons[0]
var widthConstraint = NSLayoutConstraint(item: firstButton, attribute: .Width, relatedBy: .Equal, toItem: button, attribute: .Width, multiplier: 1.0, constant: 0)
widthConstraint.priority = 800
mainView.addConstraint(widthConstraint)
}
mainView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
}
}
func addConstraintsToInputView(inputView: UIView, rowViews: [UIView])
{
for (index, rowView) in enumerate(rowViews) {
var rightSideConstraint = NSLayoutConstraint(item: rowView, attribute: .Right, relatedBy: .Equal, toItem: inputView, attribute: .Right, multiplier: 1.0, constant: -1)
var leftConstraint = NSLayoutConstraint(item: rowView, attribute: .Left, relatedBy: .Equal, toItem: inputView, attribute: .Left, multiplier: 1.0, constant: 1)
inputView.addConstraints([leftConstraint, rightSideConstraint])
var topConstraint: NSLayoutConstraint
if index == 0
{
topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: inputView, attribute: .Top, multiplier: 1.0, constant: 0)
}
else
{
let prevRow = rowViews[index-1]
topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: prevRow, attribute: .Bottom, multiplier: 1.0, constant: 0)
let firstRow = rowViews[0]
var heightConstraint = NSLayoutConstraint(item: firstRow, attribute: .Height, relatedBy: .Equal, toItem: rowView, attribute: .Height, multiplier: 1.0, constant: 0)
heightConstraint.priority = 800
inputView.addConstraint(heightConstraint)
}
inputView.addConstraint(topConstraint)
var bottomConstraint: NSLayoutConstraint
if index == rowViews.count - 1
{
bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: inputView, attribute: .Bottom, multiplier: 1.0, constant: 0)
}
else
{
let nextRow = rowViews[index+1]
bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: nextRow, attribute: .Top, multiplier: 1.0, constant: 0)
}
inputView.addConstraint(bottomConstraint)
}
}
}

Resources