MKMapView not filling view (iOS) - ios

I am creating a view programmatically. The view is fairly simple as it only contains an MKMapView which I want to fill the screen (with the exception of the navigation bar at the top).
Here is what I am doing to setup the MKMapView:
- (void)setupMapView
{
mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
mapView.delegate = self;
mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:mapView];
}
However, the MKMapView doesn't seem to fill the entire screen. If you look at the top of this screenshot there is a gap between the top of the map and the bottom of the navigation bar (which is added by the UINavigationController).
Please could someone suggest why this is happening and how I can fix it?

The frame of the view (self.view.frame) probably have an offset in the y position. This is to position it correctly in its superviews coordinate space. When you copy that frame onto your new view it is inappropriate because you are moving into a different coordinate space.
A cheap alternative that will work in 99.9% or cases is to use the view bounds instead:
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
Because this usually has an origin of zero and the full size of the view.

Related

Understanding Creating a UIView programmatically with constraints tutorial?

I'm following this tutorial:
http://matthewmorey.com/creating-uiviews-programmatically-with-auto-layout/
In it (I think) he creates a little red subview and adds constraints to the red subview.
But don't you need to add constraints to the view itself (the green part)?
Lets say you wanted to make the green view always be full screen as the image shows in his tutorial. How would you do that?
The green background will automatically fill an MDMView, whatever its frame is.
This line (already in the tutorial) sets an MDMView to be the view of an MDMViewController:
self.view = [[MDMView alloc] init];
And window.rootViewController automatically fills the window with its view:
MDMViewController *rootViewController = [[MDMViewController alloc] init];
self.window.rootViewController = rootViewController;
So, no, there is nothing more you need to do in code to get the green to fill the screen.
You will, however, need to add a Retina 4 LaunchImage asset to Images.xcassets. It should be 640 x 1136. This serves to indicate that the app should support 4-inch and larger screens. Without this asset, you'll get black bars at the top and bottom on larger screens.
What it is saying is
UIView *contentView = [[UIView alloc] init];
contentView.backgroundColor = [UIColor greenColor];
self.view = contentView;
It means the green view is main view. You don't need to set constraints for main view. It will always be full screen.

UIViewController view not size correctly when running iOS7 simulator

I have a custom UIViewController which create a view containing an action bar at the top (view with 4 buttons), a tableview and then another view below the tableview. Layout is done all in code and is not using auto layout.
Everything works perfectly on various device with iOS 7.0 and 7.0.2, but in the simulator, the root view of the controller get anchored at the top right corner of the screen (0,0) instead of below the navigation bar.
I'm going to force the relay out in the viewDidAppear: method, but this seem like a hack...
Thanks for any insights
Edit: added an image. You can see the UIView highlighted. As ManicMonkOnMac mentioned, the UIView is under the toolbar (but this only happens in the simulator, on the device, the view lines up fine)
In the loadView method on the controller, i set the frame when creating the view:
- (void)loadView
{
// Our parent view controller will resize us appropriately. The size set
// here is a convenience for initial view layout.
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
But this frame is later changed. Not by my code, though, but by UIkit code)
Edit2: addded loadView method body
EDIT: After going through session 201 of WWDC 2013, I think I have the solution.
in iOS 7 there is a property that you can set on your view controllers to specify whether you want the views to be overlapped by navigation bar.
viewController.edgesForExtendedLayout = UIRectEdgeNone;//UIRectEdgeAll specifies that nav bars should overlap the view.
Unlike iOS 6, navigation bars are placed over the views in iOS 7.
Use the frame size that excludes the navigation bar.
code:
CGRect frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y+self.navigationController.navigationBar.frame.size.height,self.view.frame.size.width,self.view.frame.size.height);
CustomView *view = [[CustomView alloc] initWithFrame:frame];
[self.view addSubview: view];

Right way to do implement customView with map

I want to create UITableView with map in beginning. Like in Foursquare app:
The trick is after scroll map should disappear like that:
How to do it? I'm thinking about using scrollview under the TableView, but maybe is a better solution. This can be done in interface builder?
Set your map view as UITableViews tableHeaderView. It will then scroll with tables content like in your screenshot
Short basic example code:
CGFloat mapHeight = 100.0f;
MKMapView *mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, mapHeight)];
// Do additional configurations of map view
self.tableView.tableHeaderView = mapView;
This code added to -(void)viewDidLoad will add 100pts in heightMKMapView to the top of UITableViewControllers table view

Storyboard white space at top in all model controllers

Am getting a white space at top in all containers. if I do a model transition that white space is clearly visible on top of all navigation controllers.
that space is around 20px.
How to remove this white space..??
Any suggestion ???
You didn't really give us enough details in your question, but the most likely cause for your problem is that you are instantiating the subviews programmatically for your viewcontrollers using the viewcontroller's view frame rather than the bounds.
If in your viewDidLoad method you do something like:
- (void)viewDidLoad
{
[super viewDidLoad];
MyView* myView = [[MyView alloc] initWithFrame:self.view.frame];
[self.view addSubview:myView];
}
then your view will be initialized with an offset of 20px, because your self.view.frame's origin is (0, 20), in order to not overlap with the navigation bar. If you then set your subview's frame with that same origin, it means that the view's origin will be 20 px further translated down (it's not really pixels but points, as the value stays the same whether it's a retina display or not, but whatever).
So, if you want to add a subview that is the size of the view controller's view that is presenting it, then you should always initialize it with:
MyView* myView = [[MyView alloc] initWithFrame:self.view.bounds];
I hope this helps, you didn't give us much to work with in your question, in the future try to give us (many many) more details about what it is that you do, both in the code and in the storyboard.

adjusting view after hiding navigation bar

I have an app that has both navigationbar and toolbar on display with various buttons...
I have an imageview that will act as a help overlay (like you see in many apps these days) that is semi transparent with arrows pointing to the buttons on the bars plus actual view content.
First attempt displays the imageview in the view area but leaving the bars in place...not good!
So next attempt I have included the bars as part of the imageview and add this to take up the entire screen, so far so good. I then hide the bars but oh no.....the view moves up 44 pixels (as expected)
Problem is no matter what I do I cannot get the view to move down the 44 pixels?
So the imageview displays the bars giving the illusion the overlay (imageview) is on top, but the view in between is out of whack!
Does anyone know how to resolve this?
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 750, 1024)];
iv.userInteractionEnabled = YES;
iv.image = [UIImage imageNamed:#"myimage.png"];
UIWindow *window = self.view.window;
[window addSubview:iv];

Resources