Using Interface Builder to add subview - iphone

My questions below are about doing this with Interface Builder, not in code.
I have a XIB with two views. One of them is default view (Table View) and I add the second (Activity Indicator View) as a subview in code. my questions are:
Is it possible to set the location of the second subview using IB, not in code? (i.e. can I remove the code line: activityIndicator.frame = CGRectMake(160, 240, 30, 30);)
Is it possible to add the second subview as a subview using Interface Builder? (i.e.e can I remove the code [self.view addSubview:self.activityIndicator])

Yes, to both questions.
The xib editor has a "Size inspector" that looks like a ruler icon near the top right of the Xcode window. That allows you to specify the size and location of a view inside its parent view. There are also controls that determine what happens to the subview when its parent changes size...on rotation, for example.
You can make a view into a subview either by dragging it into the parent in the layout section of the editor screen or by arranging views in the hierarchy window under "Objects".

Related

Hiding or Removing UIImageView and arrangement (IOS 5)

I have a UIViewController that I designed in Interface Builder. near the top of the View it has a UIImageView and then some other Views (Text etc) below. These are set to anchor to the top of the view.
In code when the view loads I remove the UIImageView in certain circumstances but it still seems to take space or the view's below don't spring up to the top based on their Anchor.
How can I make the Views below move up if the View above is removed?
You can change the frame of the other elements in the view.
in the viewDidLoad method you can use something like that:
-(void)viewDidLoad{
if(imageVisible){
otherElements.frame = CGRectMake(....);
}else{
otherElements.frame = CGRectMake(....);
}
}
I don't know what is the structure of the view, but you might consider nesting all of the other elements in one view so you could change all the elements positions in one command.
Unlike the Android view hierarchies, iOS view hierarchies are all developed in absolute coordinates. When you say a subview is anchored to the top, that means that if the size of the parent view changes, your view will stay in the same relative position to the top of the screen.
In order to make your views below the image view move up when the image is hidden or removed, you will have to manually arrange them yourself to account for the offset

Fix UIButton on top of UIScrollView

I'm trying to figure out how to overlay a UIButton on top of a UIScrollView. Basically the user would be able to use the scrollview normally. But a button in the top right of the scrollview would remain in a fixed position. Any thoughts?
Don't add the UIButton as a subView of the UIScrollView, but instead of "self.view"
In the InterfaceBuilder there will be a Hierarchal list of items you put in your view, drag the button from being a view belonging to the scrollView to belonging to the view containing the subview.
Your view hierarchy will look like the following
mainView
|
----Button
|
----ScrollView
Instead of
mainView
|
----ScrollView
|
----Button
basically It means both your button and your ScrollView will both be subViews of the exact same mainView. If your scrollView is the mainView put an empty UIView above the scrollView and insert the UIButton & UIScrollView into that
IB is a little finicky when dropping new views directly on the 'fake' interface. What you will need to do is go over to the left in the list of views and select your button view. Now drag it up to below the 'self.view' or whatever is your top level view.
YOu should notice as you raise it up and down in the list that a light blue line will appear. The width corresponds to 'subview' or 'parent' view connection. Play with it a bit to see the difference.
When you feel comfortable, you should be able to place the Button in the 'self.view', not as a subview of the scroll view. Additionally, if you want it to appear on TOP (physical 'Z level') you will need it to be below the scroll view in the list. ( this points out a subtle problem with CStreet's solution)

View table size and positioning won't change

I have a xib tableview controller and i can't get the size of the table view to shrink so that i may add additional things before the view(I would like to add some buttons). I try changing the frame in viewdidload and viewwillappear but nothing seams so work. I am using
self.tableView.frame = CGRectMake(0, 100, 320, 200);
but the view remains in place(just under the navigation bar). What am I doing wrong?
Why dont you resize in the xib tableView itself? What's preventing you to do that?
EDIT: Why cant you change the height in xib or x and y coordinates? Attaches image should help you do this easily in IB - (make sure to select your tableView and open the right hand sidebar to get this menu. Once right sidebar opens goto - Size Inspector & here you can do this.
I had the same problem and i realized that my tableView was inside another TableView with its own x and y (so probably i was not able to change it). So you have to make sure that your TableView is a subview of your view or is your main view
Use a UIViewController, drag and drop a UITableView in it, resize as per your wish and there you've got what you were looking for.

Centering the contentView within UIPopoverController

In my UIViewController that I use for my UIPopoverController, I have a label on top, a segmented control, and then a UITableView below it. When I created my .xib for the content view, I put in a table, and centered the label and segmented control within the .xib.
When I present the popover, I do not want it taking the whole screen. I use about (800,700) points to display the popover, but then the labels do not look right. They are not centered within the popover since the popover is now smaller than the .xib. What I did was move the label and segmented control to the left to make it centered in IB. That doesn't seem the best and future proof way of doing it. I was hoping I could do something programatically by taking the width size and dividing by 2 or something and centering based on that. Any advice? Thanks.
There are a couple of options:
Check out the documentation for the popoverContentSize property on the UIPopoverController, you should set it to be the size of the XIB that provides the content (so the XIB and popover both have the same size/contentSize, essentially).
Set the autoresizing masks on the subviews in the XIB so that they lay themselves out correctly dependant on the size of the XIB. To center them within their parent view you do NOT want the left and right mask setting (they should both be deselected in Interface Builder). This guide covers centering subviews: http://www.techotopia.com/index.php/IPhone_Rotation,_View_Resizing_and_Layout_Handling

Adding views to a scrolling view

I have a view (myContactViewController.xib) which contains a Scroll View (created in Interface Builder, and which takes takes up the entire height of the window and is 960px in width — ie. 3 x the width of a single window). I'm using the Scroll View, with paging, for horizontal navigation of three subviews (all three subviews are 320x460, ie. the entire size of the window). I have created these three subviews via Xcode (command-N), and so for each subview I have a .h, .m and .xib. I have used Interface Builder to build the layout (just some text and a picture for now — but, will eventually contains some buttons) for their views.
Programmatically, I would like the load these three views into my Scroll View, specifying their location within. The .xibs for each of the views I'd like to load into the scroll view are as follow: myContactSub1.xib, myContactSub2.xib and myContactSub3.xib).
I'm wondering if anyone would be willing to explain how to do this?
Thanks,
Kristin.
Sure it's very easy. First set the frame for the subview. The frame specifies where in the superview you want it to appear and is a CGRect. So you might use CGRectMake(0, 0, 320, 460) for the first subview. Then add it to the UIScrollView with addSubview:. Here's an example:
mySubView.frame = CGRectMake(0, 0, 320, 460);
[myScrollView addSubview:mySubView];
This assumes you have a pointer to these views, using IBOutlets. If you need help doing this read up on Xcode Integration
Also take a look at View Geometry