Y-coordinate of UITabBar subview - iphone

I'm creating a custom subclass of a UIViewController (without a nib), which I'm pushing onto a UINavigationController stack. Somewhere during the initialization of my UIViewController subclass (loadView? viewDidLoad? init?) I want to add a UITabBar subview to the bottom of the view. The problem is figuring out the Y-coordinate. As far as I can tell, the view gets resized somewhere after loadView, viewDidLoad, and init so I can't get the resized height in order to calculate the Y-coordinate of the UITabBar.
What is the proper way to figure out the height of the containing view such that I can anchor the UITabBar at the bottom?

What you need to do is set your UITabBar to the bottom of the view, and then tell it to stay there if the bounds of the superview changes.
This isn't too tricky. For example, in viewDidLoad alloc/init the tab-bar as normal, and position it as follows:
tabBar.frame = CGRectMake(self.view.frame.size.height-tabBar.frame.size.height, 0,
tabBar.frame.size.height, tabBar.frame.size.height);
[self.view addSubview:tabBar];
...which will add the bar to the bottom of the view.
So far so good: your problem is when the superview changes height the tabbar doesn't stay locked to the bottom. To fix this, we set an autoresizing mask:
tabBar.autoresizingMask = UIViewAutoresizingMaskTopMargin;
...which will effectively lock the bar to the bottom of the superview.

Related

Centering vertically in the viewcontroller with navigationbar in swift?

I tried to center an UIImageView in my UIViewController which has a navigation bar at the top and what I wanna do is to have my UIImageView centered between the bottom of the navigationbar and the bottom of the view (with constraints of course). But Xcode make it vertically centered in the view from top of the view (centered between top of the navigationbar and bottom of the UIViewcontroller).
Is there a way to do what I wanna do? with constraints.
Add another UIView (empty) with constraints with top and bottom layout guide. Then add your UIImageView as a subview and align vertically and horizontally.
Try calling self.edgesForExtendedLayout = [] inside viewDidLoad()
Assuming your UIViewController in embedded in a UINavigationController then you should be able to make the center of your UIImageView equal to the center of the UIViewController's view. Otherwise if you added the nav bar manually you will need to offset the center Y coordinate of the UIImageView by the height of the nav bar.

How to make tableview's scroll bar scroll in a smaller rect than table's frame?

How to make tableview's scroll bar scroll in a smaller rect than table's frame?
Like this:A UITableViewController in a UINavigationController.
Tableview's frame is (0,0,320,480)(This frame said by NSLog), its scroll bar is always under 44px. So scroll bar will not displayed behind navigation bar.
How to archive this? Special thanks
You coud set scroll edge insets. For example:
UIScrollView *scrollView;
[scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(44, 0, 0, 0)];
As UITableView inherits UIScrollView all this methods are available.

iPhone : TabBar overlaps above UIScrollView

I have created an iPhone UI programmatically but I just can't figure out how to fit a view so that it doesn't get overlapped by the TabBar. Here's the ownership hierarchy:
- AppDelegate
UITabBarController
UINavigationController x
UITableViewController
UIViewController (with XIB)
UIScrollViewController (in the same XIB as parent)
The problem is that my TabBar hides a part of my UIScrollView (and my tableviews as well, but that's not important). I tried manipulating the scrollview content size and the view frame size but with no luck.
So far the best I could do was to add a 60px padding on the scrollview's content size like this:
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+60)];
Thanks for your help!
Sounds like you may have to make sure your scrollView's frame is set properly first. The contentSize will determine the dimensions of the content of the scrollView. In other words how far you can scroll in each direction. Take a look at the scrollView in your nib and make sure the frame is the right size you need to fill.

iPhone UIScrollview: Button does not respond when located below 480 pixels on Scroll View's child UIView

I have built a view for an iPhone app in Interface Builder. It is a UIScrollview with a UIView and a UIButton on the UIView. The only code I have is setting the scroll view's contentSize to 320x550 in the viewDidLoad method of the xib's File Owner class. When the button is within the normal view area (320x480) the button responds as normal, but if is placed outside of those boundaries in Interface Builder the button will not respond when I scroll to it and click the button.
What am I missing? I figure it might be something I need to set on the UIView. But I am not sure what that is.
Your UIButton won't response even it's visible because it's not in the boundary of parent view. You can see object outside the boundary of its parent view because it's a default behavior of UIView to draw all subview (clipsToBounds = NO)
To see the truth, try this code.
UIView *yourUIView = ...
yourUIView.clipsToBounds = YES;
yourUIView.backgroundColor = [UIColor cyanColor];
You will no longer see your UIButton.
To fix this, enlarge your UIView.
I had the same problem as the person who posted the question. Thanks to the first answer, I had a hint as to what to do. I increased the height of the view by adjusting the frame. This, apparently must be done in code. Once this was done, however, the toolbar at the bottom was no longer visible. So before I adjust the height of the view, I grab the position of the tool bar. Then after adjusting the height of the view, I reset the position of the tool bar. Now all is good.

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).