how to add checkmark in UISwitch control? - ios

Hello everyone i am trying to add a checkmark on UISwitch control. I do not know where to start. Any help appreciated. Result should be like the image below. Actually i have already an implementation without the checkmark but i need to add the checkmark somehow.

The short answer is that you can't do it with UISwitch. Before iOS 7 you used to be able to set on/off images, but those properties are now deprecated. If yo don't like the default styling, you have a few options:
You could add a floating image on top of the UISwitch like #DonMag suggested. This is probably the easiest way to go, but might not end up with the exact visual treatment you want.
You can use use a UISegmentedControl or a UIButton. Both of those classes have ways to set custom images for different states. UIButton is likely simpler to customize than UISegmentedControl.
You can build your own UIControl or use an open source custom control (i.e. from Cocoapods, or other source). Going open source will be easier than building your own.

Related

iOS: Small thumb in the native UISwitch

I'm using UISwitch in my UIViewController but I don't know how can I decrease the size of its thumb. I searched about it but I only find some libraries offering a switch with a big thumb. There is no attribute in the storyboard or the code that can help on doing this. So how can I have a small thumb of the native iOS UISwitch?
You cannot change the thumb image of a UISwitch.
Check the UISwitch documentation to see what properties are configurable.
If you want a switch with a different style, you can create your own custom UIControl subclass that behaves however you wish.
From a design perspective, there are only a small number of cases where it would be beneficial to have a custom switch. The UISwitch thumb image is relatively large to create the affordance that it can be "flipped".
The default UISwitch also contains some accessibility features that you will lose if you create a custom switch.
I found a great pod that solves this, and many other problems on GitHub.
This will allow you to create switches with custom sizes, shapes, and gives you a lot more power over customization.
I just installed the class into my project, rather than using pod install, as it is only a single file class, and as of this posting was not updated to Swift 4.2.
In order to use it with Storyboards, I added a UIView of the shape and size I wanted the switch, and then added the my custom switch to the UIView.
Link to the GitHub source below. Note this is not my repo, it was just something I came across.
https://github.com/bvogelzang/SevenSwitch

Swift UIButton - How to remove underline?

I have problem when in iOS settings is enabled this setting "Button Shapes"
It causing this underline in application (first picture with enabled setting, second without)
Any idea how to programatically or in storyboard disable it?
I tried attributed text but I get same result :(
I'm newbie in Swift.
Thanks for help!
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
This is an answer by user4291543 from this question Remove underline on UIButton in iOS 7
[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];
I found this answer works with SWFrameButton
And for all the others saying "Don't Do This", SWFrameButton is a very good example of when you would want to do this. I also think the OP's situation is a perfectly valid scenario as well...
I totally agree with #maddy's comment:
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
But I did stumble on a way to accomplish the task at hand...
In addition to a UIButton, you'll also need to make a .png file that contains nothing (meaning the entire contents have an opacity of 0%). Go ahead and load that into your xcode project's assets.
Now go ahead and set the Button's Background to that image you just provided. (In my case, I called it clear) This will remove the underline from the button's text. However, now you can't see the boundaries of the button. This can be solved by changing the Background of the button's View. Go ahead and select any color for the View's Background property and now the background of the View visibly defines the button's boundaries. You're able to see this because your clear.png has an opacity of 0%.
see the Attributes inspector for UIButton here.
Rather than trying to defeat the underline by going to make a label perform some action via UITapGestureRecognizer, this allows you to still use a UIButton. Keeping inline with accessibility features to mark buttons for people that want to do that.
You could create a custom button class with a label (with clear color). If you set the text of this label instead it shouldn`t get an underline.
Are you sure you want to do that?
Apple added an accessibility feature to mark buttons for people that want to do that. Apple will probably reject your app because it defeats a system function meant to help the disabled.
I found the solution. All you have to do is set a picture as the background of the button. just pick a picture with the same color as the button you created.

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.

Create custom UIButton with multiple images and shift Label and maybe add properties

for an iPad application in ios5.0 and arc, I need to create a button that has an image covering the entire button, and needs to have another transparent image at the bottom half of this button image OR have the button text label covering the bottom half of this button image.
In posts on this site I've read that using button subclass to just change the appearance of the UiButton should not be done. However, if I don't subclass, can I add these transparent image/and shift the button label? if so, how?
In case I need to add properties to the button, what is the best way to go about it.
If subclassing is the only option, can you also pls give pointers on what are the methods that i must absolutely override and any other such memory/performance considerations that I must keep in mind
Pointers to Any tutorials or third party libraries would be most appreciated.. Thanks in advance for all your help
I don't agree on "subclassing UIButton is not good". That's exactly why inheritance and subclassing mechanisms exist. In all platforms, the framework provides a base foundation for general needs, and you do extend them in the case standard stuff does not satisfy your needs. And you do it by subclassing.
As long as you know what you do, and what you do works for you and solves your problem, you're fine.
When you subclass UIButton, depending on what you actually want to achieve, you may want to override init:, initWithRect:, layoutSubviews:, awakeFromNib: methods.
Inspecting some subclasses would also help:
https://github.com/ardalahmet/SSCheckBoxView
https://github.com/ardalahmet/CopyableCell
For UIButton, you can inspect this component. Source code may help a lot.

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