Resizing and rearranging component controls using code/ IB - iphone

Is it possible to resize and rearrange UI components (say segmented controls, buttons, labels, etc) when these are created using IB?
I need to rearrange/resize few UI components on a button click on iPad screen.
Any idea?

It is possible.
You will need to create IBOutlets for the UI components you are interested in, then you can adjust the properties you are interested in (UIView's frame property)
IBOutlet's are created in the *.h file for you View controller.
IBOutlet UIView * view;
Then you can control click and drag from the file owner (in xib) to the UI element. (or right click and drag)

Yes it is. Implement the viewDidLoad method in your controller and set the frame to whatever you wish for your views and controls so long as you've attached them to an outlet.
Example:
self.myCustomView.frame = CGRectMake(x,y,width,height);

You may also access them by their tag.
If you assign them unique tag numbers in IB, then in your code, you can search within subviews of your view and find them by viewWithTag method.

You can set the initial position of the components in IB, hook up the elements with IBOutlets and on the button click move them in code. I don't think you can rearrange them in IB.
For a cool effect you can use [UIView animateWithDuration:animations:] to move whatever components you need

Related

How To Prevent Interface Builder From Adding Items To A Custom View?

UPDATE: Edited for clarity.
This is not a showstopper, but it is annoying, and I'd like to figure out how to address it.
I have a custom UIView. A UIScrollView, to be precise, that is programmatically populated with a bunch of UIViews at runtime. It is not meant to have anything embedded in Interface Builder (IB).
What happens with any UIView, is that when you drag another element over it, the UIView becomes "droppable," and allows the other element to be dropped into it.
There are a few native Apple elements that won't let you drop stuff into them, like UIPickerView.
Is there a flag or something I can set, so that it won't allow IB to add anything to it?
Like I said, not a showstopper. If stuff gets added, it is destroyed before the programmatic population happens, but it just seems "neater" to make it clear that it's not "droppable."
UPDATE: In the screengrabs below, the Login Picker View is a standard UIPickerView, and the Display Results Scroller View is my custom scroller. If I drag something out of the Library, and try to drop it on the Picker View, nothing happens. However, I am able to drop it into my Display Results View.
What I want, is to be able to declare my Display Results View as a "no fly zone," so it is no longer droppable.
I hope that makes it clear.
Cannot Drop onto A PickerView
Can Drop On My Custom View
I'm not saying this is a great solution, but if you're using a storyboard (not a xib) then it seems to work…
In your storyboard, instead of dragging a scroll view out of the library, drag out a container view.
IB automatically creates a new storyboard scene connected to the new container view by an embed segue. Delete the newly-created scene.
Set the container view's custom class to UIScrollView.
IB does not allow you to add any subviews to a container view.
Note that IB also doesn't show you any UIScrollView-specific properties in the Attributes inspector, so if you need to customize this funky scroll view, you have to do it programmatically.
The library doesn't offer the container view in a xib, hence the storyboard requirement.

Add UIView at a UIButton

Here is the requirement of my application:
There is a calendar type view which will show Date, Day, Total work hours of that day. What I need, when I click anywhere in that particular square block then it redirects my page to a new view.
I am thinking to should add multiple button and UIView on that button.
That can solve my problem but it is little bit problematic. How should I to do this?
Please look at UITapGestureRecognizer for your view. I think this is what you're looking for.
Here is a link to a tutorial on using UITapGestureRecognizer
Create a subclass of UIControl and a XIB with a UIView in it. Set the class type for the UIView to your new UIControl (not the file owner but the UIView). Then in the UIControl in the initWithCoder: method create the display component and add to view. Then set up you properties that you need to populate those controls.
To use this new control place a UIView where you want it on your screen and size it. Change its class type (in the inspector) to your new subclass. Now you can add outlets and actions to this view just like you would a UIButton. The only downfall is can't set the display controls from the inspector, you'll have to do that in the program. But the good side is you get a similar interface to a button and you can make it look anyway you like.
Hope this helps.

How to create a toolbar in a TableView with a Label?

I would like to add a toolbar with a label entitled "Save your search?" on the left and a button "Save" on the right that triggers a specific action when tapped. How could I do that programmatically, especially I want this Toolbar to show up only when a particular View is loaded on a screen but not on every view.
Also, I want the toolbar to have a static image as background. "Save" button will also have a static image for background
Just to check; why do you need to add this toolbar of sorts onto a TableView? Depending on how you've set things up; and specially seeing that you need to conditionally hide/show this toolbar, might be easier to add it outside the tableView (just above it I guess).
Seeing as you need to hide/show this toolbar at will; guess you can simply use a UIView for it and add the UIButtons on top as subviews; --> declare it as a property in the .h file so that it can be freely accessed in the .m file whenever you need to hide / show it.
Did you need help on some specific issue related to this perhaps; or would this serve as a goo enough starting point?

Updatable, custom view on a UIToolbar

I want to make a small area to present some information in the middle of a UIToolbar and was wondering what the best way to do this is.
I need to show some text and a graphic, both of which need to be updated (around every 3 seconds) as new information arrives. The graphic is similar to the iPhone signal strength indicator, so it can be either custom drawn or selected from one of 3 graphics (low, medium, high strength).
I'll probably use initWithCustomView: to create a UIBarButtonItem, although I would like the view to be clickable (to allow the user to change the information shown).
What's the best way to make that view? Can I use a NIB, or do I need to do custom drawing in the view? What's the best way to update the buttons? I'm assuming that I'll have to remake the toolbarItems array each time and set it when the information changes. Is there a cleaner way to do this? Thanks.
Using initWithCustomView: sounds like a good way to go. You can create your custom view any way you want: with a NIB, by drawing it, even using images. It can also have its own subviews. It can be any object that inherits from UIView. (So, if you wanted, you could even make it actionable by using a UIButton, a custom UIControl, or a custom UIView with a gesture recognizer attached.)
You shouldn't have to remake toolbarItems (or, for that matter, do anything with it after you've added all your button items) if you just keep a pointer to your custom view UIBarButtonItem. It could be an instance variable. Then, to update the custom view, you could access it as you would any other view. I've never actually tried this, but I can't see any problem with doing it.
You sound like you had it mostly figured out. Hope this is helpful.
I needed the same solution and was hoping for some code examples from you. So I ended up doing it all in IB and the steps are here as follows:
Create UItoolbar in IB with no Items. (A Bar Button Item will be added again once you add the UIView)
Add UIView as subview of UIToolbar
Add UILabels to subview of UIView that is already a subview of the UIToolbar.
Create IBOutlets from UIToolbar, UIView and each UILabel and then you can reference the labels in your app.
I did set the backgrounds to clearColor so the text appears on top of UIToolbar without any box or borders.
And the text updates dynamically which was the desired outcome.
Hope this helps someone as this has been eluding me for a while.

Moving and existing UI on a UIScrollView

I have some xibs with all sorts of text controls (UITextFields, UITextViews). Since the keyboard obscures some of these text controls when text input starts, I followed Apple's guideline for managing content located under the keyboard.
In short, the solution involves moving all interface elements on a UIScrollView. Doing it from Interface Builder I simply add a UIScrollView on the view, make it the size of the view, send it to back and link it's referencing outlet to the file's owner view property.
In IB this all looks fine, and the UI elements appear above the UIScrollView. However when I run the program, the UI elements are nowhere to be found. Their IBOutlets however seem to get initialized so it looks like they are actually constructed. I've tried to set the UIScrollView alpha to 0 to see if they are placed behind it but I still can't find them. New items that are added to the UIScrollView however, seem to work fine.
This leaves me with the not so great option of rebuilding all my xibs where I need to do this change. It kind of looks like an Interface Builder bug to me. What do you guys think?
Well I've found a solution. It goes like this:
Drag a UISCrollView in IB's main window (where we have the File's Owner and First Responder objects).
Rescale it to the size of the initial view
Drag and drop everything from the original view to the scroll view.
Link the scroll view's referencing outlet to the file's owner view property.
Delete the old view from IB's main window.
#MihaiD
use tableview.contentOffset=CGPointMake(x,y);
I don't know if you really need a UIScrollView. See this question on SO it slides the parent view up.