RadioButton in swift - ios

I am new to swift and just embeded a SSRadioButtonsController in Xcode now I want this to be displayed with some text in a UIAlertView.
I have done this but it doesn't work at all.
#IBAction func infoClicked(sender: UIButton) {
self.displayAlert("My Health Action Plan Detail", message: "A Health Action Plan is personal plan describing what you need to do to stay healthy. Using the plan can help you focus your health activities.")
var radioButtonController = SSRadioButtonsController()
radioButtonController.setButtonsArray([button1!,button2!,button3!])
var currentButton = radioButtonController.selectedButton()
}
I need to display some radio buttons with text alongside in alert view

As #matt said You cannot add interface a UIAlertView or UIAlertController. And you can add ssradion button to view like this.
let radioButton = SSRadioButton(frame: CGRect(x: 0, y: 0, width: 100, height: 20 ))
radioButton.circleRadius = 8
radioButton.circleColor = UIColor.redColor()
radioButton.setTitle("1", forState: .Normal)
radioButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
let radioButton1 = SSRadioButton(frame: CGRect(x: 0, y: 0, width: 100, height: 20 ))
radioButton1.circleRadius = 8
radioButton1.circleColor = UIColor.redColor()
radioButton1.setTitle("2", forState: .Normal)
radioButton1.setTitleColor(UIColor.blackColor(), forState: .Normal)
var radioButtonController = SSRadioButtonsController(buttons: radioButton, radioButton1)
radioBtnController.delegate = self
add radiobutton to view
self.view.addSubView(radioButton)
self.view.addSubView(radioButton1)
and if you want to detect which button is clicked you need to implement SSRadioButtonControllerDelegate method didSelectButton.

Related

Fixing "use of unresolved identifier 'addTarget'" while adding func to button click event

I've created a UIButton programmatically as shown below:
let buttons: [UIButton] = [UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))];
Now if I try to add a function to it programmatically like this:
[buttons[0] addTarget:self action:#selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside]
I get an error saying that addTarget is not defined.
How do I fix this?
you are try to use the Objective-C syntax in swift, this is entirely wrong, use your code as like
buttons.first?.addTarget(self, action: #selector(self.buttonClicked(_:)), for: .touchUpInside)
and handle the action as like
#objc func buttonClicked( _ sender: UIButton) {
print("buttonClicked Action Found")
}
Ref : Apple Document for UIButton
First of all you are creating [UIButton] which is Array of UIButton and it's not a single Button.
You can not create Array of UIButton that way. You will need a for loop for that and you need to update the frame accordingly.
And you can create a single UIButton this way:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
then you can add it into the UIView this way:
self.view.addSubview(button)
Without above line it your button will not show into your screen.
Next if you want to add action to that button you can do it by adding this line in your button code:
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
and it will need a helper method which will execute when button will click.
#objc func buttonClicked(_ sender: UIButton) {
//Perform your action when button is clicked.
}
And you also need to apply backgroundColor and setTitle to the button.
and your final code will look like:
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = UIColor.green
button.setTitle("Test Button", for: .normal)
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
self.view.addSubview(button)

How to add badge to UINavigationItem (UIBarButtonItem) - Swift 3

Dose ios providing badge on UIBarButtonItem?
if not then how to do it programmatically or use any library(what are the best library)
In image i want to show badge on UIBarButtonItem (Refresh button icon) like red color round or numbers any thing.
I'm using MIBadgeButton its working like a charm.
var appNotificationBarButton: MIBadgeButton! // Make it global
self.appNotificationBarButton = MIBadgeButton(frame: CGRect(x: 0, y: 0, width: 50, height: 40))
self.appNotificationBarButton.setImage(UIImage(named: "bell"), for: .normal)
self.appNotificationBarButton.badgeEdgeInsets = UIEdgeInsetsMake(10, 0, 0, 10)
self.appNotificationBarButton.addTarget(self, action: #selector(self.appNotificationClicked), for: .touchUpInside)
let App_NotificationBarButton : UIBarButtonItem = UIBarButtonItem(customView: self.appNotificationBarButton)
self.navigationItems.rightBarButtonItem = App_NotificationBarButton

Swift Custom keyboard - show extra letters pop-up on keyboard long press?

I have a custom keyboard extension in my app which is developed using swift. They keyboard works fine. I wanted to add the functionality of showing a pop-up with extra characters when long-press on a keyboard button like the default iOS keyboard. Something like this:
I searched a lot, but most of them are un-answered and the answered ones are in Obj-C. I don't know much about Obj-C and am fairly new to swift programming also.
I have already looked at this, this and this. But these are not of much help.
Any help would be really appreciated.
1. Add Button on your View
(This is just to show you)
let btn: UIButton=UIButton(frame: CGRect(x: 5, y: 70, width: 30, height: 30))
btn.setTitle("A", for: .normal)
btn.setTitleColor(UIColor.black, for: .normal);
self.view.addSubview(btn)
2. Add Long PressGesture on your button
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(sender:)))
longGesture.minimumPressDuration = 1.2
btn.addGestureRecognizer(longGesture)
3. Handle Long press Gesture ,
You can Add PopUpView and add Some button on it ,
⚠️ Note: You have Multiple buttons so you have to check From CGPoint
on Which Button it was tapped on
func longPress( sender: Any) {
let longPressGesture = sender as! UILongPressGestureRecognizer
//Only run this code When State Begain
if longPressGesture.state != UIGestureRecognizerState.Began {
return
}
// if PopUpView is Already in added than remove and than add
if let checkView = self.view.viewWithTag(1001) as? UIView {
// remove popView
popUpView .removeFromSuperview()
}
let tapLocation = longPressGesture.location(in: self.view)
popUpView=UIView(frame: CGRect(x: tapLocation.x-10, y: tapLocation.y-65, width: 150, height: 60))
popUpView.backgroundColor=UIColor.orange
popUpView.layer.cornerRadius=5
popUpView.layer.borderWidth=2
popUpView.tag=1001
popUpView.layer.borderColor=UIColor.black.cgColor
let btn0: UIButton=UIButton(frame: CGRect(x: 5, y: 5, width: 30, height: 30))
btn0.setTitle("A1", for: .normal)
btn0.setTitleColor(UIColor.black, for: .normal);
btn0.layer.borderWidth=0.5
btn0.layer.borderColor=UIColor.lightGray.cgColor
popUpView.addSubview(btn0)
let btn1: UIButton=UIButton(frame: CGRect(x: 35, y: 5, width: 30, height: 30))
btn1.setTitle("A2", for: .normal)
btn1.setTitleColor(UIColor.black, for: .normal);
btn1.layer.borderWidth=0.5
btn1.layer.borderColor=UIColor.lightGray.cgColor
popUpView.addSubview(btn1)
let btn2: UIButton=UIButton(frame: CGRect(x: 70, y: 5, width: 30, height: 30))
btn2.setTitle("A2", for: .normal)
btn2.setTitleColor(UIColor.black, for: .normal);
btn2.layer.borderWidth=0.5
btn2.layer.borderColor=UIColor.lightGray.cgColor
popUpView.addSubview(btn2)
btn0.addTarget(self, action: #selector(self.buttonAction(sender:)),
for: UIControlEvents.touchUpInside)
btn1.addTarget(self, action: #selector(self.buttonAction(sender:)),
for: UIControlEvents.touchUpInside)
btn2.addTarget(self, action: #selector(self.buttonAction(sender:)),
for: UIControlEvents.touchUpInside)
self.view.addSubview(popUpView)
}
4. Handle The extra Button Press
(Do your Stuff here add remove popUpView from SuperView)
func buttonAction( sender: Any) {
// Do your Stuff Here
//Than remove popView
popUpView .removeFromSuperview()
}
Result ✅
✅ Note: You can Draw custom Shape of PopUpView Using UIBezierPath
You should use LongPress Recognizer. Please check this for more detail. Long press delete key of a custom keyboard in swift
This is very simple follow this step for achieve that task
Open your main story board
Select your TextField where you want multiple letter want's to show.
Open Attribute inspector from the right of your screen
Scroll it up and looks for capitalization just below of Min font size
Set capitalization as Words
Set all other Default and mainly keyboard type Now build and run that and check with letter s, e and etc.

RxSwift not working with UIButton

I am working on RxSwift and started creating few basic. I have added new button however with rx_tap subscribe not working for button action. Below is my code, please let me know what I am doing wrong
let button = UIButton(frame: CGRect(x: 10, y: 66, width: 100, height: 21))
button.backgroundColor = UIColor.redColor()
button.setTitle("Login", forState: UIControlState.Normal)
let disposeBag = DisposeBag()
button.rx_tap
.subscribe { [weak self] x in
self!.view.backgroundColor = UIColor.redColor()
}
.addDisposableTo(disposeBag)
self.view.addSubview(button)
Your subscription is cancelled immediately because of the scope of your DisposeBag. It is created, then goes out of scope and immediately deallocated. You need to retain your bag somewhere. If you are using a view controller or something like that, you can create a property there and assign it to that.

UIButton label not showing

I have the following code
let width = UIScreen.mainScreen().bounds.width
let linkLabel = UILabel(frame: CGRect(x: 8, y: contentHeight, width: 100, height: labelHeight))
let linkButton = UIButton(frame: CGRect(x: 108, y: contentHeight, width: width - 108, height: labelHeight))
mainScrollView.addSubview(linkLabel)
mainScrollView.addSubview(linkButton)
linkLabel.text = "Link:"
linkButton.addTarget(self, action: #selector(linkButtonPressed), forControlEvents: .TouchUpInside)
linkButton.setTitle("http://example.com", forState: .Normal)
linkButton.layer.borderWidth = 1.0
mainScrollView.contentSize = CGSize(width: view.frame.width, height: contentHeight)
And when view loads i see the following picture (Note that button is black rectangle):
So title is not visible but when i click on the button i can fetch its title property
func linkButtonPressed(sender: UIButton) {
print("button title = \(sender.titleLabel?.text)")
}
And i get the button title = Optional("http://example.com") but anyway button title is not visible. Any suggestions?
Your button title is display but in white color.Give any color to button title it is there.There is no problem in your code.
linkButton.setTitle("http://example.com", forState: .Normal)
linkButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
You skipped one step. You should add title color :-
linkButton.setTitleColor(UIColor.blackColor(), forState: .Normal)

Resources