Xcode swift remove button - ios

I add a button like this:
var button: UIButton!
button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(screenWidth/2-50, 40, 100, 50)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
And then I want to remove it with this code:
self.button.removeFromSuperview()
But it doesn't disappear. Why doesn't it disappears?

If you connect the button to your code by creating an IBOutlet, you can remove it from the view with:
button.hidden = true

Related

part of UIButton is not responsive to touch events

I have created a UIView with a subview of type UIButton. The button's size is bigger than the view's. Whenever I touch the button only the part that is the same size as superview is responsive to touch events. I created a drawing of view hierarchy to show you what I mean:
Only the red part of a button is responsive to touch events. Wht is this happening and how can I fix this?
This is the code in viewDidLoad, where I create the button programatically
let button = UIButton(frame: CGRectMake(-8, -8, 52, 52))
moveView.addSubview(button)
button.setBackgroundImage(UIImage(named: "trIcon"), forState: UIControlState.Normal)
button.adjustsImageWhenHighlighted = false
button.addTarget(self, action: "gogo", forControlEvents: UIControlEvents.TouchUpInside)
moveView.clipsToBounds = false
The UIView is created in storyboard.
Please follow the instruction as I tried your coding
let button = UIButton(frame: CGRectMake(0, 0, giveValueLessthanMoveViewWidthSize, giveValueLessthanMoveViewHeightSize)) //Whatever give value less than your view size(x,y,width,height)
button.setBackgroundImage(UIImage(named: "trIcon"), forState: UIControlState.Normal)
button.adjustsImageWhenHighlighted = false
button.backgroundColor = UIColor.blueColor()
button.addTarget(self, action: "gogo:", forControlEvents: UIControlEvents.TouchUpInside)
moveView.clipsToBounds = false
moveView.addSubview(button)
Then button action method
func gogo(sender:UIButton!)
{
println("Button Clicked")
}

Can't click custom added UIBarButtonItem

I've got this transparent Navigation bar, and I can add a custom icon (code below). However it does not seem to respond to clicks, anyone have any idea why this could be caused?
I'm adding this inside a UIViewController which is inside a UINavigationController.
var button: UIButton = UIButton()
button.setImage(UIImage(named: "customBack"), forState: .Normal)
button.frame = CGRectMake(0, 0, 40, 40)
button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
button.targetForAction("doAction:", withSender: button)
button.backgroundColor = UIColor.brownColor()
var leftItem:UIBarButtonItem = UIBarButtonItem()
leftItem.customView = button
self.navigationItem.leftBarButtonItem = leftItem
self.navigationItem.leftBarButtonItem!.action = "doAction:"
self.navigationController!.navigationItem.leftBarButtonItem = leftItem
I know I shouldn't be adding back items myself but in this exact case it is needed. The icon is added on the fly not before loading the view or w/e.
To try it...
var ysBackButton:UIButton = UIButton(frame:CGRectMake(0, 0, kNavBtnSize, kNavBtnSize))
ysBackButton.setImage(UIImage(named: kBack), forState: UIControlState.Normal)
ysBackButton.addTarget(self, action: Selector("backButtonAction"), forControlEvents:UIControlEvents.TouchUpInside)
var leftBarButton:UIBarButtonItem = UIBarButtonItem(customView: ysBackButton)
self.navigationItem.leftBarButtonItem = leftBarButton
Always just after posting I find out:
I was using
button.targetForAction("doAction:", withSender: button)
Whilst I should be doing:
button.addTarget(self, action: "doAction:", forControlEvents: .TouchUpInside)

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)

Programmatically Setting UINavigation Bar Button

How can I set the UINavigation Bar Button to a particular image programmatically using Swift??
let rightButton: UIButton = UIButton.buttonWithType(UIButtonType.Custom)
rightButton.frame = CGRectMake(0, 0, 40, 40) ;
rightButton.setImage(UIImage(named:"ImageName.png"), forState: UIControlState.Normal)
rightButton.addTarget(self, action: "rightNavButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
var rightBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: rightButton)
self.navigationItem.setLeftBarButtonItem(rightBarButtonItem, animated: false);
Try this this should work!!

Add labels inside of ScrollView Xcode Swift

I want to display labels and buttons inside a Scrollview.
Can anybody tell me how this is done?
#IBOutlet weak var PeopleView: UIScrollView!
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake("INSIDE SCROLL.VIEW", "INSIDE SCROLL.VIEW", 183, 21)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Button", forState: UIControlState.Normal)
button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
Use this line of code
self.PeopleView.addSubview(button)
instead of
self.view.addSubview(button)
and make sure you are passing a CGFloat value while giving buttons or labels frame

Resources