Swift, Xcode: Changing UIButton Background and Text on Click - xcode

I want to change the background and text of the button on click, I tried several SO solutions but they haven't worked, you can see what I tried in my project:
https://github.com/jzhang172/modalTest
I tried debugging it by putting a simple print statement and it looks like it doesn't ever go to it.

UIButton's have a method for setting the title color. So if you had a UIButton IBOutlet named myBtn:
myBtn.setTitleColor(UIColor.blackColor(), forState: .Highlighted)
and to change the text of the button on touch:
myBtn.setTitle("This button was touched", forState: .Highlighted)
As far as setting the background color, you could add an extension for your UIButton which allows you to do this:
extension UIButton {
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
func setBackgroundColor(color: UIColor, forUIControlState state: UIControlState) {
self.setBackgroundImage(imageWithColor(color), forState: state)
}
}
Then you could do:
myBtn.setBackgroundColor(UIColor.grayColor(), forUIControlState: .Highlighted)
Hope this helps!

SOLUTION:
1) Create an IBAction from your UIButton and also an IBOutlet called button.
EDIT: As per your request (How to trigger the even when the button is TOUCHED, not RELEASED?):
2) Do this:
#IBAction func changes (sender: UIButton) {
button.backgroundColor = UIColor.blueColor()
button.setTitle("Button Title", forState: UIControlState.Normal)
}

Your UIButton #IBOutlet closePop is hooked up to a single #IBAction of the exact same name (closePop()). That closePop() IBAction ONLY dismisses the helpViewController - it doesn't do anything about the text or button color.
Your other #IBAction function like, in which you try to set the color & print "Is this even working?", is not hooked up to the button, and is never called.

Related

How to create AppleTV buttons?

At first glance they look like regular UIButtons however they got a label below it. Also the background of the button seems to be a blurred effect.
So my thoughts are that they are put in a CollectionView (Horizontal). With each cell containing a UIButton and a UILabel. Although that may work the UIButton doesn't seem to get the move effect for free.
Is that custom behavior? And if so, how are you able to create such an effect?
I bet it is not an UICollectionView but a horizontal UIStackView of custom views in which there is a UIButton and UILabel vertically aligned.
Here you have an example, using stackViews:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .equalSpacing
stackView.alignment = .center
stackView.spacing = 30
view.addSubview(stackView)
["One", "Two", "Three", "Caramba"].forEach {
let buttonStackView = UIStackView()
buttonStackView.axis = .vertical
buttonStackView.distribution = .fillProportionally
buttonStackView.alignment = .center
buttonStackView.spacing = 15
let button = UIButton(type: .system)
button.setTitle($0, for: .normal)
buttonStackView.addArrangedSubview(button)
let label = UILabel()
label.text = $0
label.font = UIFont.systemFont(ofSize: 20)
buttonStackView.addArrangedSubview(label)
stackView.addArrangedSubview(buttonStackView)
}
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
Having a custom view instead of a vertical uistackview for each button would allow to customize its layout when focused, including Parallax effect.
For adding parallax effect to each button in the stack, take a look to How to get Parallax Effect on UIButton in tvOS?

How to change button image in swift?

I am working on an app which has a button. The button has no text, image or background.
So what I want to do is to give it an image in the viewDidLoad function.
This is what I have:
#IBOutlet var tapButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
tapButton.setImage("redTap.png", forState: UIControlState.Normal)
}
But I have an error that says I can not convert string to UIIMage.
How do I get it to work?
I have tried:
let image = UIImage(named: "redTap.png")
tapButton.setImage(image, forState: UIControlState.Normal)
I have gotten it to work but now I have a problem.
The image is suppose to be a red text but it shows up blue.
I was able to get the image to show correctly by using:
let image = UIImage(named: imageColor[randNum])?.imageWithRenderingMode(.AlwaysOriginal)
tapButton.setImage(image, forState: UIControlState.Normal)
What I want now is to not have the button be highlighted when the button is pressed. I have a few other buttons that have images assigned to them in xcode and not through code. They don't highlight when pressed.
So how can I get rid of highlighting when the button is pressed?
You don't need ".png".
If ".imageWithRenderingMode(.AlwaysOriginal)" is working for you: To keep the same image in different states, you have to set the same image/properties for the different states.
#IBOutlet var tapButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
tapButton.setImage(UIImage(named: "redTap")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
tapButton.setImage(UIImage(named: "redTap")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Highlighted)
}
Swift 3
yourBtn.setImage( UIImage.init(named: "imagename"), for: .normal)
now (swift 3 edition):
#IBOutlet var tapButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
tapButton.setImage(UIImage(named: "redTap")?.withRenderingMode(.alwaysOriginal), for: .normal)
tapButton.setImage(UIImage(named: "redTap")?.withRenderingMode(.alwaysOriginal), for: .normal)
}
First I would put the image you wanted inside the Assets.xcassets folder. Just drag it in. Then you can call it whatever you want by double-clicking on it. Lets say that it is called "redTap".
For code you would put:
#IBOutlet var tapButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let redTapImage = UIImage(named: "redTap")
tapButton.setImage(redTapImage, forState: UIControlState.Normal)
}
There's one simple solution:
#IBOutlet var tapButton: UIButton!{
didSet{
let image = UIImage(named: imageColor[randNum])
tapButton.setImage(image, forState: UIControlState.Normal)
}
Besides that, in the Attribute Inspector, Set Button type to 'Custom'
First set your button type as Custom in interface builder and set the image for normal state for your button.
Connect IBOutlet for button in class file as
#IBOutlet weak var btnCheckbox: UIButton!
in your class file in viewDidLoad set image for selected state as
btnCheckbox.setImage(UIImage.init(named: "name_of_image"), for: .selected)
Create #IBAction for in class file as
#IBAction func onTapCheckBox(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
}
Then connect #IBAction to your button's Touch up Inside event from interface builder
Let image = UIImage(named : "redTap.png")
tapButton.setImage(image, .Normal)
let button = UIButton(type: .Custom)
let image = UIImage(named:"redTap")?.imageWithRenderingMode(.AlwaysTemplate)
button.setImage(image, forState: .Normal)
button.tintColor = UIColor.redColor()
Update For latest swift versions,
It works this way
button.setImage(UIImage(named: "propopup")?.withRenderingMode(.alwaysOriginal), for: [])

1 UIImagePickerController and 2 buttons

i have an UIImagePickerController and 2 buttons. When I tap on button 1 I want to set the image of button 1. And when I tap on button 2 I want to set the image of button 2. I've successfully set the image of button 1 using UIImagePickerController but failed to do so with button 2. Here's my code:
var whichButton: Int = 0
func displayImagePicker(){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func chooseImageOne(sender: AnyObject) {
displayImagePicker()
whichButton = 1
}
#IBAction func chooseImageTwo(sender: AnyObject) {
displayImagePicker()
whichButton = 2
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
if whichButton == 1 {
self.imageOne.setImage(image, forState: UIControlState.Normal)
} else if whichButton == 2 {
self.imageTwo.setImage(image, forState: UIControlState.Normal)
}
}
As you can see I tried having a var to keep track of which button I'm tapping on. I'm not sure what I've done in the didFinishPickingImage func is right. I'm very new to swift, if anyone can shed some light it will really be great! thanks in advance
I have personally checked your code on xcode and its correct. Please see if you have given IBOutlet to self.imageTwo button and also check for the IBAction i.e touchupInside for the imageTwo button. else everything is fine
Your code is functionally correct. Looks like your #IBAction functions are not hooked up correctly, or the #IBOutlet of the second button. You could easily check this with print statements in the button handlers, or in the debugger.

Call Action when NSStatusBarButton is right-clicked

I am searching for a way to detect whenever the NSStatusBarButton is right-clicked (using Swift) and call an action.
I am currently setting it up this way:
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
if let button = statusItem.button {
button.image = NSImage(named: "myImage")
button.alternateImage = NSImage(named: "myImage")
button.action = Selector("myAction")
}
}
I thought of using the button.rightMouseDown(<#theEvent: NSEvent#>) (Because there is no such "alternateAction") but unfortunately I did not manage to come up with something due to the fact that I just started programming Mac apps.
Update:
While searching for a way to do this I saw some threads telling to subclass a NSView but I don't se how this should work (This could be because I am really new to programming and don't even know how to "subclass"). Still I thought there was some easier way to use this since nearly every statusBar App that I know rects on right-clicks.
You can subclass and override the mouseDown method, but since Mac OS X 10.10 (Yosemite), there has been an easier way: NSGestureRecognizer and its subclasses:
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
if let button = statusItem.button {
button.image = NSImage(named: "myImage")
button.alternateImage = NSImage(named: "myImage")
button.action = Selector("myAction")
// Add right click functionality
let gesture = NSClickGestureRecognizer()
gesture.buttonMask = 0x2 // right mouse
gesture.target = self
gesture.action = "myRightClickAction:"
button.addGestureRecognizer(gesture)
}
}
func myRightClickAction(sender: NSGestureRecognizer) {
if let button = sender.view as? NSButton {
// Handle your right click event here
}
}
I had the same problem as you with the accepted answer's method: it didn't work for buttonMask 0x2, only buttonMask 0x1. Regular NSButtons (but not NSStatusBarButtons) can handle NSClickGestureRecognizers, so perhaps that's what the answerer was thinking. Another solution I found suggested was to set the NSStatusItem's view to an instance of your own custom subclass of NSView, but as of OS X v10.10, getting or setting view is deprecated, so I didn't want to do that.
I solved this by adding a custom subclass of NSView as a subview of the NSStatusItem's button. My NSView implements -rightMouseUp: to receive the right mouse up event and then just passes that event to a block given to it by my class that wants to handle the right mouse click event.
Here's my custom subclass:
#import <Cocoa/Cocoa.h>
#interface TTRightClickDetector : NSView
#property (copy) void (^onRightMouseClicked)(NSEvent *);
#end
#import "TTRightClickDetector.h"
And the implementation:
#implementation TTRightClickDetector
- (void)rightMouseUp:(NSEvent *)theEvent
{
if(self.onRightMouseClicked)
{
self.onRightMouseClicked(theEvent);
}
}
#end
And here's how I use it:
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
NSStatusBarButton *button = self.statusItem.button;
button.image = [NSImage imageNamed:#"image"];
button.action = #selector(leftMouseClicked:);
TTRightClickDetector *rightClickDetector = [[TTRightClickDetector alloc] initWithFrame:button.frame];
rightClickDetector.onRightMouseClicked = ^(NSEvent *event){
[self rightMouseClicked];
};
[button addSubview:rightClickDetector];
The swift version of commanda's answer (subclassing an NSView and implementing mouseDown).
NSView Subclass:
class RightMouseHandlerView: NSView {
var onRightMouseDown: (()->())? = nil
override func rightMouseDown(with event: NSEvent) {
super.rightMouseDown(with: event)
if onRightMouseDown != nil {
onRightMouseDown!()
}
}
}
Then adding it to the status bar button and setting the code block:
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
if let button = statusItem.button {
let rmhView = RightMouseHandlerView(frame: statusItem.button!.frame)
rmhView.rightMouseDown = {
// Do something when right mouse down on button
}
button.addSubview(rmView)
}

iOS 8 - UIPopoverPresentationController moving popover

I am looking for an effective way to re-position a popover using the new uipopoverpresentationcontroller. I have succesfully presented the popover, and now I want to move it without dismissing and presenting again. I am having trouble using the function:
(void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController
willRepositionPopoverToRect:(inout CGRect *)rect
inView:(inout UIView **)view
I know it's early in the game, but it anyone has an example of how to do this efficiently I would be grateful if you shared it with me. Thanks in advance.
Unfortunately this hacky workaround is the only solution I've found:
[vc.popoverPresentationController setSourceRect:newSourceRect];
[vc setPreferredContentSize:CGRectInset(vc.view.frame, -0.01, 0.0).size];
This temporarily changes the content size of the presented view, causing the popover and arrow to be repositioned. The temporary change in size is not visible.
It seems this is a problem Apple need to fix - changing the sourceView or sourceRect properties of UIPopoverPresentationController does nothing when it's already presenting a popover (without this workaround).
Hope this works for you too!
I had luck using containerView?.setNeedsLayout() and containerView?.layoutIfNeeded() after changing the sourceRect of the popoverPresentationController, like so:
func movePopoverTo(_ newRect: CGRect) {
let popover = self.presentedViewController as? MyPopoverViewController {
popover.popoverPresentationController?.sourceRect = newRect
popover.popoverPresentationController?.containerView?.setNeedsLayout()
popover.popoverPresentationController?.containerView?.layoutIfNeeded()
}
}
And even to have a popover follow a tableView cell without having to change anything:
class MyTableViewController: UITableViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MyPopoverSegue" {
guard let controller = segue.destination as? MyPopoverViewController else { fatalError("Expected destination controller to be a 'MyPopoverViewController'!") }
guard let popoverPresentationController = controller.popoverPresentationController else { fatalError("No popoverPresentationController!") }
guard let rowIndexPath = sender as? IndexPath else { fatalError("Expected sender to be an 'IndexPath'!") }
guard myData.count > rowIndexPath.row else { fatalError("Index (\(rowIndexPath.row)) Out Of Bounds for array (count: \(myData.count))!") }
if self.presentedViewController is MyPopoverViewController {
self.presentedViewController?.dismiss(animated: false)
}
popoverPresentationController.sourceView = self.tableView
popoverPresentationController.sourceRect = self.tableView.rectForRow(at: rowIndexPath)
popoverPresentationController.passthroughViews = [self.tableView]
controller.configure(myData[rowIndexPath.row])
}
super.prepare(for: segue, sender: sender)
}
}
// MARK: - UIScrollViewDelegate
extension MyTableViewController {
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let popover = self.presentedViewController as? MyPopoverViewController {
popover.popoverPresentationController?.containerView?.setNeedsLayout()
popover.popoverPresentationController?.containerView?.layoutIfNeeded()
}
}
}
I used the same method as mentioned in another answer by #Rowan_Jones, however I didn't want the popover's size to actually change. Even by fractions of a point. I realized that you can set the preferredContentSize multiple times back to back, but visually it's size will only change to match the last value.
[vc.popoverPresentationController setSourceRect:newSourceRect];
CGSize finalDesiredSize = CGSizeMake(320, 480);
CGSize tempSize = CGSizeMake(finalDesiredSize.width, finalDesiredSize.height + 1);
[vc setPreferredContentSize:tempSize];
[vc setPreferredContentSize:finalDesiredSize];
So even if finalDesiredSize is the same as your initial preferredContentSize this will cause the popover to be updated, even though it's size doesn't actually change.
Here is an example for how to recenter the popover:
- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView **)view {
*rect = CGRectMake((CGRectGetWidth((*view).bounds)-2)*0.5f,(CGRectGetHeight((*view).bounds)-2)*0.5f, 2, 2);
I have also used this method to ensure that the popover moved to the correct location after moving by setting the *rect and the *view to the original sourceRect and sourceView.
As an additional note, I don't believe that this method is called when the popover's source is set using a bar button item.
I'm posting this because I don't have enough points to vote or comment. :)
#turbs's answer worked for me perfectly. It should be the accepted answer.
Setting *rect to the rect you need in the delegate method:
(void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController
willRepositionPopoverToRect:(inout CGRect *)rect
inView:(inout UIView **)view
iOS 12.3
[vc.popoverPresentationController setSourceRect:newSourceRect];
[vc.popoverPresentationController.containerView setNeedsLayout];

Resources