Hiding or Removing UIImageView and arrangement (IOS 5) - iphone

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

Related

what will happen when addSubView a big view into a small View

There is a xib with a a dark view (called calendarView) in it
Next is I am adding a subView called calendar ( this is a calendar view from this page )
[self.calendarView addSubview:calendar];
And the result is :the calendar is added but it is cover the calendarView
My question :
what will happen when you try to add a bigger view into a small view.
How to make a bigger view fit in the the small view.
in general the subview will draw itself bigger then the superview. if you dont want that to happen you should set the subview's frame to smaller or equal to the superview's bounds.
if the subview is a costume class you made, you should probably change it to be in the right size, otherwise I think the best way to do it is with transform
Nothing exceptional. The bigger view will be placed accordingly to it's frame.
Simple, just by equaling their sizes (and the added view's origin to be (0,0) ).

How to add view like this

As shown click the top three buttons will pop up a view on the basic view. What the view is? CALayer? Or just a small size UITableView? How to implement this?
the control is like a UIPopoverController - but UIPopoverController is limited to iPad only. here's a source which have generic/custom implementation that will help you creating similar view.
https://github.com/werner77/WEPopover
I belevie this is a custom view that contains a table view inside it. it is very simple to implement that, As this is not a full tutorial I can imagine the basic steps I would do:
the shape could be a combination of layers or more reasonable drawn with Quartz2D:
I would do it this way:
create view and draw the frame with a BezierPath in the drawRect:
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
draw the top black bar and the title on the top.
draw the top triangle and add to the view a property that will set the position according to the button position. then you could conditionally change the draw position of the triangle.
add a sub view of a table view and assign the view as a DataSource and a Delegate of it, and then do all the table view implementation.
Good luck
It's just a custom view. There is probably a UITableView embedded in the view for the friend request list.

Overlaying a UIView on top of my existing view?

I have a view i want to overlay on top of another. It will be smaller and not take up the whole screen, and so it's important that the view behind can still be seen. How do i do this?
If you lay another view on top of an existing view and set the rect containing the new view to some size smaller than the screen, the view in back will still be seen. Just call initWithFrame:(your CGRect size) then your [mainView addSubview:newview]. This should add your new view on top of your existing one.

Cocoa-Touch: Can I have multiple views per view-controller, or specify bounds of a uiview?

Here's a hypothetical question:
Say I subclass a UIView that draws a triangle and I want this triangle uiview to part of the screen. And then say I subclass another UIView that draws a rectangle and I want the rectangle to take part of the screen.
In other words:
Can a view-controller have multiple views simultaneously being drawn on the screen
If so, can I set location bounds for these views. Say I want a view 50x50 on the left side of the screen etc?
Can I specify the bounds via interface builder?
A view controller owns a view hierarchy, not just a single view. However, that hierarchy must have a root at some top level view, which ends up being self.view for the view controller.
The view that is self.view need not have any direct content to display. It can simply be a UIView that holds other views, your rectangle and circle. The root view should be large enough to cover (really be under) any other views it contains. All the views in the hierarchy may be laid out in interface builder.
yes
yes
yes

Difference between directly setting a controller's view and adding view as a subview

I have just started learning objective-C and the iphone sdk and I have a question that I hope someone can help shed some light on.
What is the difference in the following:
self.view = someView;
and
[self.view addSubView: someView];
Say for example, in a simple app, where we have only one controller and one container view (has a few image subviews).
What is the difference between the two statements? The reason that I'm asking is because I was tinkering around in some sample code and I noticed the view was being initialized with images as subviews like so:
if (self = [super initWithFrame:CGRectZero])
{
//adds some images as subviews here
}
As I understand it the initWithFrame: CGRectZero, creates a frame with size at [0,0,0,0] (essentially invisible).
When I directly set the view with
self.view = someView;
I notice the view actually displays the image. But when I add the view to as a subview of controller's 'default' view, it doesn't. So basically my question is, whats going on behind the scenes? Why is the first method "resizing" the frame and the second one not doing the same thing.
What you see on the screen of your iPhone is almost always a hierarchy of views.
When you look at, say, your inbox in Mail, you're seeing a bunch of views. There's a big containing view.[1] Within that, there's a navigation bar view, a table view, and a toolbar view. Within the navigation bar view, there's a button view on each side and a label view in the middle. Inside the table view, there are a bunch of table cell views, and each of those cells has several label views. The toolbar has five button views. I could go further and talk about the views inside those buttons and so on, but I'm sure you get the idea.
The view above any given view is its superview; the views below it are its subviews. So a table cell view has a table view as its superview and a bunch of label views as its subviews. The top view, the one that has all the other views inside it, is called the root view.
Each view has its own drawing surface. The rectangle formed by that drawing surface is called the frame. The frame of a view is relative to the frame of its containing view. So if one of our table cell's label subviews has its frame at (0,0), that means it will be in the table cell's top left corner, even if the cell is halfway down the screen.
When you're writing a view controller, self.view is that root view I mentioned earlier; all the other views are subviews of that one (or subviews of its subviews, etc.). One of the features of a view controller is that it automatically resizes its self.view to fit the screen. (The available area will be smaller in the middle of a phone call: the status bar is twice as high then, so there's less space for your app. It will also be smaller if your view controller is being managed by a navigation controller or tab bar controller, but that's a different story.) But just because you resize its root view doesn't mean that the root view's subviews will automatically resize. To do that, you need to set their autoresizing mask (a property which tells the view how it should react when its superview changes size):
someView.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleHeight;
(There's a graphical way to set up the autoresizing mask in Interface Builder—click the ruler icon in the inspector window and look at the "Autosizing" section.)
Even that's not enough, though, if someView isn't the right size to start with. To do that, adjust its frame before you add it as a subview of self.view:
someView.frame = CGRectMake(
0, // all the way to the left
0, // all the way at the top
self.view.frame.size.width, // same width as the root view
self.view.frame.size.height, // same height too
);
So why would you ever use subviews if you have to do all this twiddling that the root view does for you? Simple: you can only have one root view, but one view is almost never enough for what you need to do. If you really need only one view, of course, you can just set it as the root view and go on your merry way, but chances are, things are more complicated than that.
[1] I'm simplifying a bit here, but that's fine for right now.
When you add a view as a subview, you need to make sure that you're actually adding to an existing view.
self.view = view sets the controller's view. Without this (either in code or done with a XIB) you'll never see anything as the controller has no view to show.
[self.view addSubView: someView] assumes that self.view is already set. If it doesn't, you're adding someview as a subview of nil, and it will never get seen.
Basically, think of self.view as the big container, and all the subviews are just pieces inside of it. If you don't need any subviews, setting self.view to a UIImageView or UIWebView is fine. If you do need subviews, you'll need a big, empty container view in which to put them.
In your case, I'm betting self.view is never set, and you're adding your image views to nil.
Setting the view controller "view" property only changes the view it is managing.
Adding a view as a subview of another view, actually adds the subview underneath the other view.
They are very different things, as one adjusts a view controller and the other alters a view hierarchy.
As a guess, the reason you didn't see anything the first way was the frame for the subview you were adding was CGRectZero (0 in size).