Set the action button graphic to a UIButton - ios

I know the action button graphic is for a UIBarButtonItem but is it possible to set that image to a UIButton? I already have the action set up, it's just the graphic now.
Just to specify i want to set this image to a UIButton programmatically.

There isn't a built-in way to do this, like there is for UIBarButtonItem. You'll need to create the icon yourself (in Sketch/Photoshop/whatever), and use that instead. Alternatively, there are plenty of vector icon sets available from various sources that will include this icon (a quick google will find one).

Related

Image in Button for Xcode is showing only tint color on old version [duplicate]

So, I'm a relatively new coder and understand this may be a real simple fix but I can't seem to find how anywhere, just that people know it happens. It is also possible I've misunderstood some other core idea and that is why I can't seem to fix.
I've set up a UIKit button in my storyboard, added both an IBAction and IBOutlet for this button in my script (class? whatever the individual .swift files are called within a project), set the IBO to inherit from UIButton, all in the ViewController class. In the viewDidLoad function I have
button.setImage(UIImage (named: "logo"), for: .normal)
and another line of coder pertaining to a UILabel (everything is fine there). Below the viewDidLoad function I have an array and then the IBA with a little bit of code that shuffles the array and spits out 0 indexed spot onto the screen.
I've been trying to fix this for a few days now and assuming I've understood everything I've read, I've learned that UIImages are immutable (can't be changed) so that tells me that I need to fix this when declaring it in viewDidLoad. I've also learned that it being blue is default in the current swift/xcode state, and that it is a render mode issue, just turning that off will fix the problem. I can't for the life of me connect the last few dots and figure out how to actually tell Xcode to simply use the image I've provided it.
I'm looking for a code solution of course, but the knowledge of why it will work and any corrections/clarifications to what I think I've learned will be even more appreciated so I can help myself more in the future.
Thank you in advance.
-Cyre
Since you haven't specified, I'm assuming the button's "Type" is set to "System" in the storyboard (the default button type when you drag out a new UIButton in storyboards).
By default, system buttons take any image set on it and apply a template rendering mode to it, which makes the image render all non-transparent pixels as a flat color (e.g. the tint color of the button in this case, which is by default the system blue color that you're seeing).
UIImages by default have a renderingMode of automatic, which means the actual effective rendering mode the image takes on is determined by whatever is using the image (in this case, the UIButton).
You can, however, instantiate a UIImage with a renderingMode of alwaysOriginal so that it never is treated as a template image. There are a couple of ways to do this.
1. Programmatically
You can create a new image object with a new rendering mode in code by taking the image you're already creating:
UIImage(named: "logo")
and calling withRenderingMode(_:) on it to return to you a new UIImage object that you then set on your button:
let logoImage = UIImage(named: "logo")
let logoImageAlwaysOriginal = logoImage?.withRenderingMode(.alwaysOriginal)
button.setImage(logoImageAlwaysOriginal, for: .normal)
2. Asset Catalog
An easier way than going the programmatic route above is to just change the image's default rendering type in your asset catalog. Click on the image you want to change in your asset catalog ("logo" in this case), then go to the Attributes Inspector (keyboard shortcut: option+command+4), and change the "Render As" attribute to "Original Image". Now, whenever you use or get this image from the asset catalog (either in your storyboards or in code), the image will have the rendering mode of .alwaysOriginal.
You can either do any of the above options and keep the system button type, OR you can just change your button's type to "Custom" in the storyboard, as custom buttons do not apply a template rendering mode to their images by default. However, you lose some nice things when not using the system button type, like the text color no longer is taken from the button's tint color, and the button doesn't have a nice fade animation when unpressed.

how to create my own radio buttons

I am new to Swift and Xcode so I need help with what I am doing. And what I am doing is this.
I have three buttons one of them starts out checked. and what I want it to do is when I click on another button it checks it and unchecked the previous. I want my own radio buttons
For Radio Buttons there is nothing that comes built in.
You can use SSRadioButtonsController
You can create a controller object and add buttons array to it like
var radioButtonController = SSRadioButtonsController()
radioButtonController.setButtonsArray([button1!,button2!,button3!])
You can also use something like this tutorial.
The standard Apple class that is designed to work with radio button groups is NSMatrix. Specifically you'd create an NSMatrix with mode "NSRadioModeMatrix". NSMatrix takes an NSCell for its list of items. So you can use any built-in NSCell subclass (like NSButton) or anything else custom you would like.
You can manage manually by setBackgroundImage and setImage property. at initial time set rounded image as setBackgroundImage for all three button. At button click event set "bulletin" image by setImage property for selected button and set nil for all other button setImage property.

What is this iOS Contol and How To Implement it?

I'm confused about:
1) What type of control is the one shown in the picture above. I tried slider, it's not giving that same visual effect
2) How can I implement such a control exactly as seen on the image.
It could be anything, but if i wanted to do it i would do it as a normal custom UIView, that has 3 UIButtons
This control will have a function like, setLevel:1,2,or 3
depending on the integer it will change the image of the UIButton accordingly
1) If you just want those 3 points(Low, High, Medium) you can do as #Omar suggested in his answer.
2) But if you want all intermediate points and the middle (blue) dot as a handle, you can use UISlider and change its minimumValue, maximumValue and thumb images.. See this tutorial
You can have two images one for selected and other for unselected and when any of the button is pressed change the image of button to selected image in button action and other two button will have unselected image.At a time only one button will be in selected state.
Now predefined iOS control have this presentation.
May be you can find in cocoacontrols.org or binpress.com to see if you can download a third controls made by developers.
Also, you can create yourself the control, on the two previous website, you can surely find a lot of control which mimic the display you want. You can download the source code, reverse engineering and build your own control
At worst, build this control from scratch with a UIView (maybe a UISlider) as super class.

Alternative to setTintColor for pre-iOS5 UIBarButtonItems?

are there any alternatives to using the accessor setTintColor for UIBarButtonItems in order to add ios4 compatibility?
is it possible to modify the setter to include a conditional statement for systemVersion (without subclassing UIBarButtonItem)?
There are three approaches to do this. First, is to draw a button with CoreGraphic. Second, use a custom image. The third approach is probably the easiest and allows you to use a tintColor property for dynamic changes if wanted.
The idea is to create a UISegmentedControl and stylize it to look like a button and use that tintColor property to change the color of the "button".
You can find specific details at this webpage http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem .

How to make custom buttons in iOS?

The default iOS UI is nice but if I wanted to use images for buttons instead how would I do that - is it OpenGL?
Make the button a "custom button" type in interface builder. Then set the background image to be whatever you want the background to be.
Unfortunately the default button style is a little boring. You'll need to find or make a button with an image editor.
UIButton has a lot of support for custom buttons. In the nib (or storyboard or whatever), just click on the button and set its "Type" to "Custom" in the attribute inspector on the right side of the screen. Then, just below that, set the State Config to the sate you'd like (default, selected, etc) to customize, and set Image attribute to the image you'd like.
That image must be part of the project first before it will show up in the Image attribute (or in the app). You can add the image to the project simply by dragging it into the file navigator on the left side of the screen.
You can use images for buttons using a few simple lines of code:
// code to set image for button at normal state
[myButton setImage:[UIImage imageNamed:#"buttonImage.png"]
forState: UIControlStateNormal];
You can also use Xcode to change the type to "Custom" then you can set images for each button "state."
You can read more about UIButton states here
You can simply use PNG files that you create yourself. The UIButton class allows you to use custom images for all the different states of the application.
You don't really need to do custom rendering yourself, but if you really want, you can use UIKit to draw the images yourself in code (no OpenGL involved): http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/graphicsdrawingoverview/graphicsdrawingoverview.html
You can do it by setting image or backgroundImage properties of a UIButton.
If you want your button to look nice when the size changes, you can use stretchableImageWithLeftCapWidth:topCapHeight: method of UIImage, with an appropriate image.
You may also want to use a custom component if it satisfies your need.
its very easy
change this two thing.Change "type" round rectangle to custom and drag image to project and select it as background .Run and Go
Well so far i was too looking for using custom buttons and the graphics, I've come through this site they have some awesome tutorial which was quite helpful for me. I've watched all the video tutorial and read some books but nothing helped me but this site and their 4 books are quite awesome and really helpful to read and understand it well.
http://www.raywenderlich.com/27191/learn-to-code-ios-apps-4-making-it-beautiful

Resources