Making Universal ViewControllers
I am in the开发者_如何学Python process of developing a universal iOS app.
What I want is for all my views to automatically resize depending on the screen sisze, I mean I dont want to hard code any CGRect
sizes or use Interface Builder; I am using a specific app example but I would very much appreciate an answer that is usable in many similar scenarios.
This is a very simple app but I hope the answer can show me how to go about doing this for all my views, so they can adjust to any size.
For this specific app I am building something that will look like this:
This is a particularly tricky example as the MKMapView has to be initialized with a frame but I hope you can help me.
The first problem I ran into was the fact that in my loadView method:
MKMapView *mapView=[[MKMapView alloc ] initWithFrame:CGRectZero];
mapView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view=mapView;
The autoresizing mask does not work unless the view controller is managed by a UINavigationController
; I do not know why.
The next problem comes when I want to add my UIToolbar at the bottom of the screen.
In order to get this I do the following:
UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 420, 320, 40)];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:toolBar];
This works perfectly with an iPhone but it does not with an iPad (the toolbar's width is adjusted but it obviously is no loger at the bottom of the screen) as the size is hard coded.
Sorry for posting such a long question but I think the answers can be a great resource for me and other developers in need.
Please tell me If I am not clear.
Some of the functions defined in CGGeometry
are useful when dealing with iOS view geometry.
Say you wanted to take the entire frame and divide into two parts. The bottom part should be the toolbar frame which sits at the bottom of the screen, and is 44 px high. The remaining portion at the top should all be given to the map. A useful function for doing this is CGRectDivide
which is defined as:
void CGRectDivide (
CGRect rect,
CGRect *slice,
CGRect *remainder,
CGFloat amount,
CGRectEdge edge
);
You pass it the full CGRect, two uninitialized CGRect's that will get populated with the correct frame sizes after dividing, an edge (left, right, top, bottom) from which to start, and the distance from that edge.
Playing with unicode art, this is the best I can do. The full rect is divided into two parts. The black lines at the bottom is the toolbar's frame. The white rectangles is the frame that the map gets.
▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫▫ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
CGRect mapFrame, toolbarFrame;
CGRectDivide(appFrame, &toolbarFrame, &mapFrame, 44, CGRectMaxYEdge);
// map is an MKMapView
map.frame = mapFrame;
// toolbar is a UIToolbar
toolbar.frame = toolbarFrame;
Here's how it looks on the iPhone and iPad simulators.
For the toolbar you can work out its frame like this:
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGRect toolbarFrame = CGRectMake(CGRectGetMinX(applicationFrame), CGRectGetMaxY(applicationFrame) - 44.0, applicationFrame.size.width, 44.0)
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
If your UIToolbar shouldn't extend the width of the screen, but instead a view, just set applicationFrame to the frame of that view.
精彩评论