Custom UISwitch with image [closed] - iphone

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to implement a custom switch in a project. Currently what I have found is that we cannot alter with UISwitch in way to implement as shown in below image. I've followed posts and googled through stackoverflow and other blogs but didn't found a solution as desired. One way is to go with UISegmented control as in this post, but that too doesn't solves my problem.
Thanks in advance for any help

It's not hard to create your own switch. A UISwitch is a control — essentially just a view that sends a message — with two states. You could set it up your own custom control like this:
container view: a simple view with rounded corners (set the cornerRadius of the view's layer) and a background color
left image: an image view that displays the image you want to show on the left side, i.e. the check mark for your example
right image: an image view that displays the image you want to show on the right side, i.e. the X mark for your example
slider: an image view showing the slider portion of the switch, set above the other two image views
When the user taps or swipes the control, use Core Animation to move the slider to the other side of the switch and update the state of the control and do a quick fade to the background color for the new state. Send the control's action to the target.

As gasparuff says, you can also do it with a UIButton, just set the images:
[button setImage:[UIImage imageNamed:#"image_on"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"image_off"] forState:UIControlStateNormal];
and when it's tapped just toggle the selected property.
- (void) buttonTap {
button.selected = !button.selected;
}
Which will change the images automatically.

You know about -[UISwitch setOnImage:] and -[UISwitch setOffImage:], right? Also -[UISwitch setTintColor]. About the only drawback I can see is that the switch, itself, will be the standard iOS switch (round button) but, otherwise, this would be the most-iOS-like solution.
When implementing your own on/offImage, remember the size restrictions, and also that the side of the image toward the switch is concave. tintColor can be used to make the rest of the switch reflect your color scheme. You might want to track the valueChanged event so alter the tint color.
If you need the exact look you display in your question (square switch button), then you probably want to use a custom control, as #Caleb suggested.

It looks like you just want your control to have only 2 states - enabled and disabled.
An easy way for this would be to create 2 png-images and use a custom UIButton with a background image and just replace those 2 images each time the button is tapped.
Is this what you are trying to do or did I misunderstand something?

Related

Any way to adjust a button built with interface builder to reverse when tapped?

I am not a huge fan of interface builder but I am trying to create a UIButton that would highlight when touched. I was thinking the whole button to reverse. I see this property reverses on highlight but what it does is to reverse the text. I want the whole button to reverse color.
This is how I have it now:
This is a green button with rounded corners. Is there a way to make the whole button to reverse from interface builder?
This is an iOS 7 only project.
If you want to keep things inside interface builder, I believe your only option is to configure different background images for each button state. I had the same problem and couldn't find a way to use different UIColors for different button states. If your button style is really minimal, you could potentially use 1x1 pngs and apply rounded corners using Quartz directly in code.
In my case, I ended up creating a custom subclass of UIButton with a setter to configure different colors/text colors and shadows for each state (I used an enum to map different button "themes").

How can I create my own UITabBar? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Further to this question here, I'm really interested in creating my own UITabBar, and 'rolling my own'. Im really curious as to how this is done, especially in an app such as the Twitter app.
How can I do this? Can anyone point to any good resources on how to subclass this?
Should I do it programmatically, or in a XIB?
Edit: I'm looking to do this with custom icons/selected icons and having icons only.
Edit2: After reading further answers, it sounds like a custom XXTabBarcontroller is what I actually want to do here. Can anyone comment on this?
Thank you,
I don't know if there is a best way to do this, but I will share my experience:
I created my own version of a tab bar by subclassing UIView as opposed to UITabBar. As the linked question mentions, Apple's UI classes are quite finicky about their sizing and I do not think I could get the UITabBar subclass to size as I wanted.
Because I did not have a subclass of UITabBar I also ended up rolling my own versions of UITabBarController and UITabBarDelegate, with interfaces that are essentially the same as the Apple classes. I WAS able to use UITabBarItem to store the title and icon for the buttons on the tab bar, which is useful since UIViewControllers have a tabBarItem property. That lets you store just an array of UIViewControllers in your controller, as opposed to arrays for both controllers and tab bar items.
Because I was using mostly custom classes I did this all programmatically, including creating, configuring and laying out the buttons on the tab bar.
Like I said, this is just my experience, hope it helps.
I did this recently in an application. There was so little "code" to it that there is barely anything to post. I did it as such:
Created a UIImageView (about 50 px high), and laid it out at the bottom of the screen. I filled it with a tab-bar look-alike image - i.e. some sort of grey gradient. I probably subclassed UIImageView - but you don't even have to do that.
I drew a bunch of buttons/icons - 37x37 each.
I placed a bunch of UIButtons in the "tab bar" - and made them "Custom" views, with the buttons/icons I had created.
I simply used the touchUpInside methods to make them do stuff.
When I needed to get fancy - I'd attach the buttons to my code via and IBOutlet, so I could "disable" them - and I'd draw a greyish "disabled state" image for them.
If you want the same functionality, I'd recommend using a UITabBarController, hide the tab bar itself, and then create your own navigation. I've done this a few times and it works quite nicely.
Use something like this to hide your tab bar and expand the content view.
- (void)makeTabBarHidden:(BOOL)hide
{
if ( [tabBarController_.view.subviews count] < 2 )
return;
UIView *contentView;
if ( [[tabBarController_.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
contentView = [tabBarController_.view.subviews objectAtIndex:1];
else
contentView = [tabBarController_.view.subviews objectAtIndex:0];
if (hide)
{
contentView.frame = tabBarController_.view.bounds;
}
else
{
contentView.frame = CGRectMake(tabBarController_.view.bounds.origin.x,
tabBarController_.view.bounds.origin.y,
tabBarController_.view.bounds.size.width,
tabBarController_.view.bounds.size.height - tabBarController_.tabBar.frame.size.height);
}
tabBarController_.tabBar.hidden = hide;
}
Then make your own navigation with UIButtons to switch between your view controllers.
[tabBarController_ setSelectedIndex:0];
I think this is what you are looking for... should be pretty easy to subclass it. gl and cheer!
http://www.rumexit.co.uk/2010/07/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-1-of-2/

UITableView scrolling performance with CALayer + mask

This is not really a question because I just solved the problem, but I think the nature of the problem is common for many iPhone devs and the solution wasn't really plainly obvious so I wanted to share my experience. To keep with the StackOverflow "question/answer" format, I'm going to put the details into the question and my solution into the answer.
Details:
I had a tableview full of cells with two labels and a custom toggle switch control I put together from scratch. If I hid the toggle control, the tableview scrolled like a champ. However, with the toggle control shown, scrolling was painfully choppy / slow on the iPhone 3G. My tableview cells have no transparent components (other than the mask for the layer) and I'm reusing cells.
The custom toggle control extends UIButton and it's layer has two components - a UIImageView that contains the sliding "switch" part of the toggle and an elongated ellipse that is used as the mask, with a background drawn behind the layer in -drawRect:.
How did I fix it? Answer coming shortly...
I found out that the source of the slowness / choppiness was coming from having to re-composite the Layer with the mask sublayer on scrolling. I guess it was having to re-draw the toggle switch every time the screen was re-drawn.
My solution is to hide the toggle switch layer/mask unless it's changing state. When it's sitting fully in the on or off positions, I hide the toggle switch layer and replaced it with an image of the toggle switch. When the user touches the toggle, the dummy image hides, the actual toggleswitch component shows and does the animation to the opposite state. When the animation finishes, I hide the toggle switch component and show the dummy image, making sure to change the image to the current state. This improved scrolling performance SIGNIFICANTLY...I dare say that it's almost as good as a native default tableview cell.

What are your favourite UITableView / UITableViewCell tricks? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
UITableView is a very powerful class, powering many navigation and preference views on iPhone. Many people have come up with useful UITableView tips, tricks and samples:
various ways to use Interface Builder for table cells
how to create preference-style cells
ensuring good scrolling speed
etc.
Please post your favourite tips on using UITableView, one tip per question. I'll start by posting the ones I found on Stack Overflow and the ones from my bookmarks.
Ever wondered what UITableViewController really does?
In viewWillAppear, it deselects any selected rows, with animated:YES.
This by the way is why when you navigate back in UINavigationController, the row you've previously touched is nicely deselected with animation. When you pushed a new view controller onto UINavigationController, you've left the row selected. When you pop it and go back to the table view, viewWillAppear fires and deselects the row. UINavigationController does not even know about this happening.
In viewWillAppear, it calls reloadData if the table view contains no rows.
In viewDidAppear, it calls flashScrollIndicators.
It monitors the keyboard appearing and disappearing and resizes the table view appropriately so that when you tap a text field in a table view, it remains visible after the keyboard appears.
If you don't need the keyboard monitoring behaviour, it is fairly easy to do everything else yourself if you need to.
Implementing “checkmarks” in editing mode to manipulate several rows at once: “Multiple row selection and editing in a UITableView” from the great Cocoa With Love blog.
How to implement a custom cell background view?
An excellent sample class by Mike Akers in “How to customize the background/border colors of a grouped table view?” on Stack Overflow.
If you want to remove the separator lines, use this:
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
To ensure good performance scrolling, it's important to avoid transparency in any element if possible - so if you are creating custom cells make them all opaque and set the backgrounds correctly.
You can use the Core Animation Performance Tool to visually see how much transparency you have going on in a cell - you have to be running on the device to use this tool.
Implementing Preferences-style grouped tables: Three20 library (extracted from Facebook iPhone app) has a set of ready-made cells that contain various controls.
(Not sure you want to use them, however. Three20 suffers from “not-invented-here” a little bit and tries to subclass and extend everything, so adding it adds quite a bit of a mess to your project. But at least you can use it as an example.)
How to use Interface Builder to author your cells? Find answers on a separate Stack Overflow discussion: “Using Interface Builder for UITableView’s”

iPhone dev: adding an overlapping label to the image

I'm trying to figure out a best way to implement the picture-editing capability shown in the native address book app on iPhone.
On the built-in address book, this is how the picture looks like before editing:
qkpic.com/2f7d4 http://qkpic.com/2f7d4
And after clicking edit, notice how "Edit" overlay is added and the image becomes clickable:
qkpic.com/fb2f4 http://qkpic.com/fb2f4
What would be the best way to implement something like this? Should I make the image a button from the beginning and have tapping disabled at first? If so, what steps are required to add an overlay/label to the image (in above example, gray border + the text "Edit" is added)
The easiest way is to use Interface Builder, create a container view, then add a UIImageView and UILabel as subviews to it. You would position and style the text and the image but set the UILabel to hidden. Make the whole container view respond to touches. It's easy to do since UIView is derived from UIResponder so all you have to do is override touchesEnded. Whenever you want to change the text label, just set the UILabel to hidden=NO.
There's more to it, however. Notice how the image has rounded corners? You'll want to override the UIImageView's drawRect method to implement a custom drawing routine to do that. There's lots of sample code around and it wasn't part of your original question so I'll stop here.