How do I make a button to dismiss keyboard
the key is located above the keyboard?
custom keyboard
var button: UIButton = UIButton()
required init(globalColors: GlobalColors.Type?, darkMode: Bool, solidColorMode: Bool) {
super.init(globalColors: globalColors, darkMode: darkMode, solidColorMode: solidColorMode)
self.addSubview(self.button)
button.setTitle("Hide", forState: UIControlState.Normal)
self.button.addTarget(self, action: Selector("tappedbutton"), forControlEvents: UIControlEvents.TouchUpInside)
button.setTitleColor(UIColor(red: 24.0/100, green: 116.0/255, blue: 205.0/205, alpha: 1.0), forState: UIControlState.Normal)
self.updateAppearance()
}
override func layoutSubviews() {
super.layoutSubviews()
self.button.frame.origin = CGPointMake(self.button.frame.origin.x + self.button.frame.width + 1, self.button.frame.origin.y)
}
Related
I am trying to create a game that requires to create a textfield in SKScene. I have built the text field successfully, but bringing the keyboard down is the problem. This is my code so far.
import SpriteKit
import GameplayKit
import UIKit
class GameScene: SKScene {
var sceneFrame : CGRect = CGRect()
var textFieldFrame : CGRect = CGRect()
var textField : UITextField = UITextField()
var skView: SKView = SKView()
override func didMove(to view: SKView) {
sceneFrame = CGRect(origin: .zero, size: CGSize(width: 200, height: 200))
let scene = SKScene(size: sceneFrame.size)
scene.backgroundColor = UIColor.lightGray
textFieldFrame = CGRect(origin: .init(x: 100, y: 200), size: CGSize(width: 200, height: 30))
textField = UITextField(frame: textFieldFrame)
textField.backgroundColor = UIColor.white
textField.placeholder = "Phone Number"
textField.keyboardType = .numberPad
textField.resignFirstResponder()
self.view!.addSubview(textField)
self.view!.presentScene(scene)
// Where to put the textField.resignFirstResponder() ?
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
Any answers would be helpful.
The problem is you are not adding:
textfield.delegate = self
Then you could call the function:
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hides the keyboard
textField.resignFirstResponder()
return true
}
Declaring the delegate statement initiate the function textFieldShouldReturn.
Hope this helps :)
I want to set my Switch like this:
But I try in ios9 , it does not work.
I saw in apple UISwitch Class Reference.
It says that :
Discussion
In iOS 7, this property has no effect.
How about iOS 9? Any one success?
My Code:
switch1 = UISwitch(frame:CGRectMake(self.view.frame.width/2 - 20, 400, 10, 100))
switch1.on = true
switch1.onTintColor = UIColor.lightGrayColor()
switch1.tintColor = UIColor.greenColor()
switch1.thumbTintColor = UIColor.blackColor()
//set on/off image
switch1.onImage = UIImage(named: "on-switch")
switch1.offImage = UIImage(named: "off-switch")
Use a UIButton instead.
let switchButton = UIButton(type: .Custom)
switchButton.selected = true
switchButton.setImage(UIImage(named: "on-switch"), forState: .Selected)
switchButton.setImage(UIImage(named: "off-switch"), forState: .Normal)
Use switchButton.isSelected instead of switch1.on. You'll have to toggle switchButton.isSelected when it is tapped, which you can do like this:
switchButton.isSelected.toggle()
For iOS 13, you could do this way:
let switcher = UISwitch()
switcher.addTarget(self, action: #selector(pressed), for: .valueChanged)
#objc func pressed(sender: UISwitch) {
let color = UIColor(patternImage: UIImage(named: sender.isOn ? "on.png": "off.png")!)
if sender.isOn {
sender.onTintColor = color
} else {
sender.tintColor = color
sender.subviews[0].subviews[0].backgroundColor = color
}
}
NOTE: your image should look like:
Then the final result is:
Not an exact answer to your question, but if you want a completely custom button switch programmatically (that you can add text to), this will work too:
import UIKit
class RDHiddenVisibleButton: UIButton {
// Hidden / Visible Button Function
var isOn = false
override init(frame: CGRect) {
super.init(frame: frame)
initButton()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initButton()
}
func initButton() {
layer.borderWidth = 2.0
layer.borderColor = Colors.radiusGreen.cgColor
layer.cornerRadius = frame.size.height/2
setTitleColor(Colors.radiusGreen, for: .normal)
addTarget(self, action: #selector(RDHiddenVisibleButton.buttonPressed), for: .touchUpInside)
}
#objc func buttonPressed() {
activateButton(bool: !isOn)
}
func activateButton(bool: Bool) {
isOn = bool
let color = bool ? Colors.radiusGreen : .clear
let title = bool ? "Hidden" : "Visible"
let titleColor = bool ? . white : Colors.radiusGreen
setTitle(title, for: .normal)
setTitleColor(titleColor, for: .normal)
backgroundColor = color
}
}
I am having trouble getting the data to load correctly! The top cells load well then when I scroll down and back up the once dark icons that had to phone number are lit and when they are touched they call a random number. This is odd, as the other cells with a phone number do not change only the ones that are left equalling "".
This is from the ViewController.swift
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contactList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
let contact = contactList[indexPath.row]
cell.name.text = "\(contact.name)"
cell.id.text = "\(phoneFormat(contact.callPhone))"
if contact.callPhone != "" {
cell.callButton.setImage(UIImage(named: "phone.png"), forState: UIControlState.Normal)
cell.callButton.userInteractionEnabled = true
cell.callButton.tag = indexPath.row
cell.callButton.addTarget(self, action: "call:", forControlEvents: .TouchUpInside)
}
/* if contact.smsPhone != nil {
cell.smsButton.setImage(UIImage(named: "chat.png"), forState: UIControlState.Normal)
cell.smsButton.userInteractionEnabled = true
cell.smsButton.tag = indexPath.row
}
if contact.email != nil {
cell.emailButton.setImage(UIImage(named: "email.png"), forState: UIControlState.Normal)
cell.emailButton.userInteractionEnabled = true
cell.emailButton.tag = indexPath.row
}*/
return cell
}
this is the tableviewcell.swift file
class TableViewCell: UITableViewCell {
#IBOutlet var name: UILabel!
#IBOutlet var id: UILabel!
#IBOutlet var callButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}}
The reason that you see this behavior is that your if is missing an else branch, in which you clear out the content of visuals being set in the if:
if contact.callPhone != "" {
cell.callButton.setImage(UIImage(named: "phone.png"), forState: UIControlState.Normal)
cell.callButton.userInteractionEnabled = true
cell.callButton.tag = indexPath.row
cell.callButton.addTarget(self, action: "call:", forControlEvents: .TouchUpInside)
} else {
cell.callButton.setImage(nil, forState: UIControlState.Normal)
cell.callButton.userInteractionEnabled = false
cell.callButton.tag = 0
cell.callButton.hidden = true
}
This would prevent buttons from reused cells scrolled off the screen from showing up in cells for which the phone number is missing.
When I press the play again button it doesn't work. I just have this code involving the play again button. What should I add here? How can I add a function to playAgain button? Here is my code:
let playAgain: UIButton = UIButton(frame: CGRectMake(-10, 400, 400, 150))
playAgain.setTitle("Play Again", forState: UIControlState.Normal)
playAgain.titleLabel!.font = UIFont(name: "Helvetica", size: 50)
playAgain.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
playAgain.tag = 1
self.view!.addSubview(playAgain)
Implement a function with any name you like, like this:
func buttonPressed(sender: UIButton) {
// do something
}
Then set this function as the action of your button:
playAgain.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
I am trying to create a customised UIButton. This customised button shows an unchecked box which will turn into a checked box, when the user presses the button. It will then return back to unchecked when user presses the checked box.
I have a runtime error. Debugger shows that it terminates because of an uncaught exception of type NSException.
Where does it goes wrong?
Code for the checked box
import UIKit
class CheckBox: UIButton {
//Images
let checkedImage = UIImage(named: "checked")
let unCheckedImage = UIImage(named: "unchecked")
//bool property
var isChecked:Bool = false {
didSet {
if isChecked == true{
self.setImage (checkedImage, forState: .Normal)
}
else {
self.setImage(unCheckedImage, forState: .Normal)
}
}
}
override func awakeFromNib() {
self.addTarget(self, action: "buttonClicked", forControlEvents: UIControlEvents.TouchUpInside)
self.isChecked = false
}
func buttonClicked(sender:UIButton) {
if(sender == self) {
if isChecked == true {
isChecked = false
} else {
isChecked = true
}
}
}
}
If you want to use selected attribute then you can do the following:
class CheckBox: UIButton {
//Images
let checkedImage = UIImage(named: "checked")
let unCheckedImage = UIImage(named: "unchecked")
//bool property
override func awakeFromNib() {
self.addTarget(self, action: "buttonClicked", forControlEvents: UIControlEvents.TouchUpInside)
self.setImage (checkedImage, forState: .Selected)
self.setImage(unCheckedImage, forState: .Normal)
}
func buttonClicked(sender:UIButton) {
if(sender == self) {
self.selected = !self.selected
}
}
}