I'm making a custom keyboard using swift on Xcode.
I have created some views(xib files) which are of keyboards.
and I wanna connect these views each other like when I press [123]button, the view changes to the numeric one so I can type numbers :)
Actually I finished globe button(which is next keyboard button which is auto-created when I added custom keyboard target) but can't understand the codes ;-;
What I want to do is to connect button(as an IBOutlet or IBAction) with view(xib)
Take two views in xib or storyboard and IBOutlet them. One view has the buttons for alphabets and other view has buttons for numbers.
You have to do code something like below :
#IBOutlet var alphabelView: UIView!
#IBOutlet var numericView: UIView!
#IBOutlet var toggleBtn: UIButton!
btn.setTitle("ABC", forState: .Normal)
btn.setTitle("123", forState: .Selected)
toggleBtn.addTarget(self, action: #selector(self.toggleButtonsClicked(_:)), forControlEvents: .TouchUpInside)
Numeric view will be hidden initially. When user press on 123 do code like below lines of code:
func toggleButtonsClicked(button: UIButton) {
button.selected = !butto.selected
alphabelView.hidden = true
numericView.hidden = false
}
Related
I'm just trying to simply set the background image of the button, but whatever I try it is doing nothing. I also put a UIImageView there to see if there's any issue with the image file, but the image view is set perfectly fine. Do I have to set something in the properties of the button?
Here the code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageToSet = UIImage(named: "3-1")
b1.setBackgroundImage(imageToSet, for: .normal)
imageView.image = imageToSet
}
#IBOutlet weak var b1: UIButton!
#IBOutlet weak var imageView: UIImageView!
}
These are my setting for the button:
In Xcode 13 ,UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on (If you set one of them 4).So when you try to use old behaviours like
setBackgroundImage(..)
setImage(..)
This functions not gonna effect on this button. So If you want to use old behaviours you need to set style Plain to Default
I'm building a simple app in Swift, and the app contains a png image, a UISegmentedControl, and a UIButton. Selecting different controls on the UISegmentedControl resizes an image of a pizza. When I click on "Large" in the UISegmentedControl, and then I press a button on the interface called submitOrderDisplay, it shrinks the image from its large size to its small size. I know it has something to do with the setTitle method I am using at the very bottom of the code, but I don't understand why that causes the image to reset to its default small size. If I remove the setTitle method, the resizing does not occur after I click the UIButton. I don't want the image to change size after the button is pressed. How do I achieve that?
import UIKit
class SecondViewController: UIViewController{
#IBOutlet weak var sizeChoiceControl: UISegmentedControl!
#IBOutlet weak var submitOrderDisplay: UIButton!
#IBOutlet weak var myImageView: UIImageView!
#IBAction func sizeSelected() {
if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Small"){
myImageView.setHeight(height: 85)
myImageView.setWidth(width: 85)
myImageView.setX(x: 146)
myImageView.setY(y: 63)
}
else if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Medium"){
myImageView.setHeight(height: 100)
myImageView.setWidth(width: 100)
myImageView.setX(x: 139)
myImageView.setY(y: 55)
}
else if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Large"){
myImageView.setHeight(height: 115)
myImageView.setWidth(width: 115)
myImageView.setX(x: 131)
myImageView.setY(y: 48)
}
}
#IBAction func SubmitOrderClicked(sender: UIButton) {
submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Normal)
}
}
Write the configuration of the button inside the viewDidLoad function:
submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Normal)
I understand that you want to put the title Update Order.
If you want to change the title when pressed, you must call it for another State of the button like:
submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Highlihgted)
If you change the configuration of the button for different states when an event happens, that must be why the size is ressetting.
I am trying to update a button with a test value and I have noticed that every second update the button title text shows the test value for a fraction of a second but then resets to the Button's default value.
It seems to be a bug, but I wanted to see if there is a simpler explanation.
I have tried waiting up to 10 seconds before pushing the button but this seems to be consistently occurring.
Any ideas how to make UIButton function as expected?
import UIKit
class ViewController: UIViewController {
var testEntry = "its working"
#IBOutlet weak var testButton: UIButton!
#IBOutlet weak var testLabel: UILabel!
#IBAction func runTest(sender:
UIButton) {
// The button value should equal the value of the label value, but every 2nd button press of the test button results in the title of the button value resetting to the default value
dispatch_async(dispatch_get_main_queue()) {
self.testLabel.text = "\(self.testEntry)"
self.testButton.titleLabel?.text = "\(self.testEntry)"
}
}
Here is the github project.
You shouldn't be directly setting the text of the button title label, you should only set the font directly onto the label. The text should be set by calling
func setTitle(_ title: String?, forState state: UIControlState)
The text toggles because you're selecting and de-selecting the button, which is switching between some of its states which have different titles.
You should set the button title text by using the following method...
self.testButton.setTitle(self.testEntry, forState: .Normal)
instead of titleLabel property.
Swift 5
self.testButton.setTitle("hello", for: .normal)
So, I have 20 UI buttons on a single viewcontroller. I also have 2 other buttons there that will change the currentTitle or background color of different groups of those buttons. (for example, pressing one of those 2 buttons will change the color of 10 other buttons, while pressing the other button will change the background of 15 of those buttons.)
Am I doomed to having to create 20 different IBOutlets like:
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button3: UIButton!
.
.
.
and then having to write an additional 25 lines of:
button1!.setTitleColor(UIColor.redColor(), forState: .Normal()
button2!.setTitleColor(UIColor.redColor(), forState: .Normal()
button3!.setTitleColor(UIColor.redColor(), forState: .Normal()
Surely theres an easier way to do this? I can't link more than one UI with IBOutlet, and changing the tags for the buttons don't do much either because I still have to write the darn things down 25 times regardless.
Is it possible to put those buttons into groups where I can write a single line of code to change ALL things in that group?
Set your button tags in series like 101 to 125 and on click of any button perform this operation
for(UIButton *button in <parent view of buttons>.subviews){
if(button.tag >= 101 && button.tag <= 125){
button.setTitleColor(UIColor.redColor(), forState: .Normal()
}
}
For Swift
for subview in self.createBugView.subviews {
if let button = subview as? UIButton {
// this is a button
if(button.tag >= 101 && button.tag <= 125){
button.setTitleColor(UIColor.red, for: .normal)
}
}
}
So I am using a custom View (.xib) which contains couple of objects: UIImage view & UIButton. The custom view in contained and used in View controller UIView , however I can't seem to figure out how to detect if the button(from .xib) is tapped and launch a function in the view controller. Any suggestions?
I can However add UITap gesture to the entire Viewcontroller UIView, however Thats not what i need.
#IBAction func calculateTapped(sender : AnyObject) {
//You can get action here
}
Step 1
Link Your all button to to xib cocoa class
class DialogHandler: UIView {
#IBOutlet weak var acceptBtn: UIButton!
#IBOutlet weak var closeBtn: UIButton!
}
step 2
Add Below code in viewDidLoad()
if let customView = Bundle.main.loadNibNamed("DialogDemo", owner: self, options: nil)?.first as? DialogHandler {
dialogView = customView
customView.acceptBtn.addTarget(self, action: #selector(ViewController.acceptBtnPressed(sender:)), for: .touchUpInside)
}
step 3
Create Function for Button that can handle your click event
#objc func acceptBtnPressed (sender:UIButton) { print("Button Pressed") }