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

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 {

Related

Admob Banner at the top of the screen

I want to have the Admob banner at the top of the screen instead of the bottom. This is what i copied from the Admob Documentations.
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}
I don't know how to work with contraints programmatically. Can anybody please help how I can put the banner at the top. Thanks!
This will work for you.
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .top,
relatedBy: .equal,
toItem: topLayoutGuide,
attribute: .bottom,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}

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.

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

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 ?