Changing UIToolBar (appearance) at runtime - ios

I want to change an UIToolBar at runtime. In it's initial state, is has only one button, when that button is pressed i want it to change it's appearance to show 4 buttons. One of these buttons should cause the first UIToolBar to reappear.
Im seeing two approaches:
1) Have two UIToolBar nibs, and load them as needed.
2) Having all buttons on the first UIToolbar, and hide/show them as needed.
What would be the correct approach?

Personally, I would want to see all 4 button at initial launch with only relevant button in enabled state and rest in disabled state. Once I tap on the already enabled button I should see other buttons getting enabled. This is less surprising UI for end user. However, you can also go with #2 mentioned above in which case you might want to add some animation effect for better user experience.

The second approach would be better, because if you want to add more buttons tomorrow, you need to maintain 2 nib files instead of one.
But, think again is creating toolbar in xib file good solution?
I would create custom toolbar extending UIToolbar class and make 2 methods in it:
-(NSArray*) toolbarButtonsInitial;
-(NSArray*) toolbarButtonsExtended;
-toolbarButtonsInitial method returns UIBarButtonItems for initial state
-toolbarButtonsExtended method returns UIBarButtonItems for second state.
IMHO, this way has several advantages:
Your xib file doesn't have hidden buttons, or some button above other
one
If you need to add or remove some buttons you can do that easily for
each state
You can easily reuse this toolbar on other screens and create new
states if necessary

Related

Place image on top of all "layers" on iOS screen

What are the constraints/alternatives to place an image that stays on top of all iOS "layers" and windows. Think of it as a lock screen but that still allows you to interact with you phone, meaning browse, answer calls, etc. The image will be displayed in a transparent way (say 40%) and will be launched by an application.
Not really sure what you are asking for but if you are looking for a way to add some code once and it shows everywhere on each of your ViewController then there really is no way to do that.
What you can do is
Use UIToolBar
add buttons to that tool bar
Add that tool bar to each ViewController in storyboard (where you need them)
Create a global function / method that you can call in from any ViewController that has the button actions in it. That way you edit the code once in one place and use it every where.
If you don't like UIToolBar then
you can add your own UIImageView and add a transparent background to it
Add your buttons on the UIImageView. (You will have to add constraints)
then show that on every ViewController
If all this seems to much work then you can use existing controls HERE and see which one fits your needs.
This maybe a good one - FCVerticalMenu

What is the correct approach for a custom UIToolBar?

Is it better - better in the sense of not getting rejected from the Apple store - when trying to create a custom* UIToolbar to either:
Option A
Add the oversize middle button as a UIButton to the self.navigationController.view this is key as I'm adding a UIButton to the navigationController which seems to contradict the apple docs
Use UIToolbar appearance to implement the custom background
Option B
Create a UIToolbar and add it as a subview of the current ViewController in UINavigationController
Add the UIButton as a subview of the current ViewController in UINavigationController
Use UIToolBar's method setBackgroundImage to add the custom background
*Custom background, Oversized middle button
This is the shape of the toolbar:
Extra details: This app is for iOS 5.X or greater. It uses UINavigationController. Key challenge is that a section of the app hide/shows the navigationbar depending on the state of the app.
Option A means I don't have to workout where to place the UIToolBar, it's a challenge to work out where to place the UIToolbar because a) if I'm showing or hiding the navigationBar this shifts everything up/down vertically b) iPhone 5 with extra vertical space - I can't use autolayout as I'm support iOS 5 and I haven't really worked out how to use autoresizing masks. I'mu using hard coded "magic" numbers.
I wouldn't do either of those.
UIToolbar is nice when you want to do what it does, and it does allow some basic customization. But in this case you want a toolbar-like view that draws itself in a way that UIToolbar doesn't seem to allow for. You probably know exactly what items you want to appear in your "toolbar", and it's unlikely that you'll need to handle arbitrary assortments of UIBarItems.
So, instead of giving yourself a headache trying to force UIToolbar to do something that it was never designed for, just create a simple view containing your background image (part of which will be transparent). Place some image-based UIButtons in the view, and call it a day. It wouldn't be a bad idea to create a custom UIView subclass representing your toolbar so that all the setup is nicely packaged up in one place, and so that you can reuse the toolbar in multiple views if you need to.

ios navigation buttons

I'm doing a custom nav in ios for the first time. I have six buttons laid out in a row. When I tap the button, I want the image to change. However, the button is not togglable. The only way a button can be unselected is if another button is touched. Only one button can be active at any given time.
My idea:
use UIButtons
change UIButton image on touch
keep track of the active button inside the navigation class
when an inactive button is touched, make the currently active button inactive and turn the touched button to active
I want the end product to work like a custom TabBarController, but without switching layouts. I just want to edit the content in the current ViewController.
Is there a better way to do this?
You could just use the UISegmentedControl, which has that functionality already. If you need to significantly customise the look and feel though, your UIButton solution sounds fine.
I setup the UIButtons in the Interface Builder like so:
Default with unselected background image
Disabled with selected background image
On touch, a button is self.enabled = NO which makes the UIButtons look change. However, the button goes dim, so I also implemented self. adjustsImageWhenDisabled = NO.
This way a button can't be re-selected once it's "active".

How can I create a dynamic overlay for a UIButton?

My application has a few portions that have really big buttons (640x130, 230x150, etc.) What I need is to have a way to update different portions of the button, with different text. Initially, I assumed that in my code I could create various UILabels and then add them as subviews to my button. However, as soon as I try to add a UILabel to the button as a sub-view, my app crashes.
What is the easiest way to create an overlay for a button, that I can completely layout myself, without preventing button taps from being interested using overlay controls?
I imagine there are multiple ways to solve this problem. However, the best solution for my case should use the fewest lines of code (I have quite a few of these types of buttons) and I'd like to be able to continue using some form of configurable button within IB.
I'm not opposed to subclassing UIButton but, if I do, I would like to be able to use it in IB. I've never created a custom UIView for such a circumstance, so I'd need help defining that type of subclass so that it will work correctly in IB.
You need to add the subview to the containing view - not the button. To ensure that is doesn't interfere with button presses, be sure to set it to:
[myCustomTextOverlay setUserInteractionEnabled:NO];

Make green color UIActionSheet button?

I know I can access a individual button on a actionSheet using a for loop to access the particular button in the action sheet I want, but the thing is, how would I make a green button?
I know each button acts almost identically to a UIButton so how should I go upon making one of my buttons in the action sheet green?
I just need some tips or help with this as it is the last part of my app that isn't done!
Thanks!
When I have needed customizations for UIActionSheets, sometimes I find methods to make it happen simply, but more often I end up having to set up a custom view on way or another. I've added all manners of custom controls with:
[sheet addSubview: myCustomActionSheetController.view];
You might need to set the size as well.

Resources