How to add UIButton Programmatically from a NSObject Class - ios

I am trying to create a UIButton in a NSObject class. I get the error " Use of unresolved identifier view. I figured that it needs to have UIViewController for it to work.
is there a work around so that I create multiple buttons with just this on multiple view controllers. what am I missing?
func createButton () {
let button = UIButton();
button.setTitle("Add", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.frame = CGRectMake(15, -50, 200, 100)
view.addSubview(button)
}

Declare method definition with parameter-argument in which you want to add button :
func createButton (view:UIView) {
let button = UIButton();
button.setTitle("Add", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.frame = CGRectMake(15, -50, 200, 100)
view.addSubview(button)
}

Related

Add a badge to UIButtonitem with MIBadgeButton-Swift

I'm trying to add a badge to my UIBarButtonItem and for that I have found a this github :
MIBadgeButton-Swift
but I don't know how to use it.
This is my code which makes my custom UIBarButtonItem:
let shopingCartBTN = UIButton(type: UIButtonType.Custom)
shopingCartBTN.setImage(UIImage(named: "shopingCarBarIcon"), forState: UIControlState.Normal)
shopingCartBTN.imageView?.image = UIImage(named: "shopingCarBarIcon")
shopingCartBTN.frame = CGRectMake(0, 0, 60, 30)//Just increase the width of button
shopingCartBTN.setTitle("5", forState: .Normal)
shopingCartBTN.addTarget(self, action: "", forControlEvents: UIControlEvents.TouchUpInside)
let customBarItem = UIBarButtonItem(customView: shopingCartBTN)
self.navigationItem.leftBarButtonItem = customBarItem;
How could I use the MIBadgeButton-Swift to make a badge for my UIBarButtonItem?
Here is two example with custom view and from storyboard
From storyboard by setting custom class :
CODE
#IBOutlet var btnRightBadge: MIBadgeButton!
override func viewDidLoad() {
super.viewDidLoad()
//Custom
let badgeButton : MIBadgeButton = MIBadgeButton(frame: CGRectMake(0, 0, 40, 40))
badgeButton.setTitle("T1", forState: UIControlState.Normal)
badgeButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
badgeButton.badgeString = "1";
let barButton : UIBarButtonItem = UIBarButtonItem(customView: badgeButton)
self.navigationItem.leftBarButtonItem = barButton
//From Storyboard
btnRightBadge.badgeString = "5"
}
OUTPUT
You can also use ENMBadgedBarButtonItem-Swift
Try this once.
//Property
var cartBarbuttonItem:MIBadgeButton?
self.cartBarbuttonItem = MIBadgeButton(frame: CGRectMake(40, 5, 40, 44))
self.cartBarbuttonItem?.initWithFrame(frame: CGRectMake(40, 5, 40, 44), withBadgeString: "0", withBadgeInsets: UIEdgeInsetsMake(15, 2, 0, 15))
self.cartBarbuttonItem?.setImage(UIImage(named: "test"), forState: .Normal)
self.cartBarbuttonItem?.setImage(UIImage(named: "test"), forState: .Selected)
self.cartBarbuttonItem?.addTarget(self, action: Selector("loadCart"), forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.setLeftBarButtonItem = self.cartBarbuttonItem

Adding a button to mapView and tapping on it throws EXC_BAD_ACCESS

On SKMaps for iOS, I'm trying to add a button to implement a "find me" functionality. The button is rendered properly on the map, but when I tap on it, it throws an EXC_BAD_ACCESS error in something called IBGMethodSwizzler
Any ideas if this is SKMaps related?
Where I initialise my map, I use this code
let findMeButton = UIButton(frame: CGRectMake(53, 90, 44, 44))
findMeButton.setBackgroundImage(UIImage(named: "find_me"), forState: .Normal)
findMeButton.addTarget(self, action: Selector("findMe:"), forControlEvents: UIControlEvents.TouchUpInside)
self.mapView.addSubview(findMeButton)
And this is the findMe selector
func findMe(sender:UIButton) {
println("this is never called")
}
Is there a known limitation to adding a UIButton as a subview of mapView?
Thanks :)
Try the following code snippet:
var positionMeButton: UIButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
positionMeButton.frame = CGRectMake(10.0, CGRectGetHeight(self.view.frame) - 60.0, 100.0, 40.0)
positionMeButton.setTitle("Position me", forState: UIControlState.Normal)
positionMeButton.addTarget(self, action:"positionMe", forControlEvents: UIControlEvents.TouchUpInside)
positionMeButton.backgroundColor = UIColor.lightGrayColor()
positionMeButton.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin
positionMeButton.titleLabel?.adjustsFontSizeToFitWidth = true
self.mapView.addSubview(positionMeButton);
internal func positionMe() {
}

How to change element title inside UITableViewCell?

I have a button on custom tableviewcell. I've been able add action to this button by add target like below code:
var playButton = cell.contentView.viewWithTag(5) as? UIButton
var sender : UIButton = UIButton()
playButton?.addTarget(self, action: "playPost:", forControlEvents: UIControlEvents.TouchUpInside)
playButton?.layer.setValue(object["previewUrl"] as? String, forKey: "songUrl")
playButton?.layer.setValue(indexPath.row, forKey: "index")
But, how can I change it's title whenever I clicked this button?
If your function is playPost(), you could change the value passed in (which is the sender of the function, by default) like this:
func playPost(sender: UIButton) {
sender.setTitle("New title", forState: UIControlState.Normal);
}
You can Change UIButton title by using setTitle Method
Example
var button = UIButton(frame: CGRect(x: 50, y: 50, width: 200, height: 100))
button.backgroundColor = UIColor.blackColor()
button.setTitle("Start", forState: UIControlState.Normal)
button.addTarget(self, action: "pressButton:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
func pressButton(sender:UIButton)
{
if sender.titleLabel?.text == "Start"
{
sender.setTitle("Stop", forState: UIControlState.Normal)
}
else
{
sender.setTitle("Start", forState: UIControlState.Normal)
}
}

Change text color of UIButton in CalloutAccessoryView

I use this code to create a UIButton in my CalloutView.
var button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.redColor()
button.setTitle("No", forState: UIControlState.Normal)
button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
anView.rightCalloutAccessoryView = button
How can I change the text color of UIButton?
You can change the title/text color of a UIButton like so:
button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

Set button type

I am creating a button programatically and I would like to know how to set that button's UIButtonType.
This is how I am creating my button:
let headerPlusButon:UIButton = UIButton(frame: CGRectMake(5, tableView.frame.width - 30, 20, 20))
headerPlusButon.addTarget(self, action: "paymentAddButtonTapped", forControlEvents:.TouchUpInside)
There is no "setButtonTypeFor..." method like how you would change the title's text and obviously this doesn't work:
headerPlusButon.buttonType = UIButtonType.ContactAdd
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonAction(sender:UIButton!)
{
println("Button tapped")
}
from swift 2 replace UIButton declaration with below line.
let button = UIButton(type: .System) as UIButton
Here is my code
let tutorialButton = UIButton.init(type: .infoDark)
tutorialButton.frame = CGRect(x: self.view.frame.size.width - 20, y: 42, width: 20, height: 20)
tutorialButton.addTarget(self, action: #selector(self.showTutorial), for: .touchUpInside)
view.addSubview(tutorialButton)
I hope it can help you!

Resources