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
Related
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.
I am about to customize all of my UIButtons by subclassing them into a special UIButton class and I want to have the "look and feel" like below octagonal or some hexagonal buttons. What would be the best way to create such shape UI buttons? So far, I only know how to create rectangle, circular and rounded-edge buttons, and I could not find SO answers or cocoapods for below "look and feel".
Is it possible to create a custom class of such UIButton programmatically?
Or do I need to import an image onto a button to realize below "look and feel"?
Also, could there be any possibility be that App Store does not allow such custom shape buttons?
Yes you can use UIBezierPath to create arbitrary shapes for a variety of UIKit components that includes UIButton. See tutorial here
I think the above is achievable simply using #1 above in combination with myButton.layer.borderColor/.borderWidth
I am not sure. My guess would be no but I think it would depend on the Apple Human Interface guidelines which seem to be arbitrary in some cases. For what it's worth, I've shipped apps that had very similar buttons but they were elipses and not polygons like you have but still appear very similar.
Update: Forgot about a great example. This is an example of a "bookmark tab" effect I created using a UIButton with a UIBezier path. This has been shipping for years at this point with no issues:
How do I create a progress indicator as shown in swift for iOS? Tried various libraries. But nothing fits exactly.The color should be in gradient and the current state must show the step number.
If you want to create any custom control element You should inherits your class from UIControl. You can read about this here:
custom knob,
custom slider.
But You will have to write too many lines of code to create customize view.
However, You can use my turnkey solutions: https://github.com/vladislovshilov/StepView
Unfortunately this library does not support touch yet.
Here is an example that uses a UIStackView, works nicely with auto layout, is animatable, and is configurable in Interface Builder.
https://gist.github.com/Dev1an/aa54ae331f4065569dc613d7f904bd54
I'm trying to build a simple peg jumping game (see: http://cl.ly/2x0v1V3z351t0j3d3y0L).
My plan is to make each "peg" a UIButton and toggle the UIButton states for the different images that need to be displayed at different times during the game. Using the default states for UIButton gets me pretty far but I need to add one extra state beyond the default 4.
How would I go about doing this? I figure I will need to sub class UIButton to create a custom UIButton object but this is where I get stuck. I can't find any resources on adding new button states. Is this something that is possible?
This link below was a major help to me implementing my own custom button. I basically took that guy's code and made it ARC compatible, but the basic principle in what he's done really works great!
Custom Button States
Subclass UIBUtton and add a custom state variable, then use the setter to change the image that is shown.
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.