How do I make UIPickerview adjust to screen size? - iphone

I have a picker that I have created in my iPhone app, and it runs fine - but the picker is part of an overlay, so some of the values are hard-coded as to how far down and to the left the overlay appears.
So an obvious error occurs when I try and make the comparable iPad version. I opened the iPad storyboard and set everything up, but naturally when I run it, I have the uipickerview appearing in the middle and to the left of the huge iPad screen, wayyy off where it should be.
Is there any way to make an overlay of a picker automatically center and hug the bottom of the screen, no matter what size the screen is, instead of having the location of it hard-coded?

It looks like you'd like the picker to rest at the bottom of the view controller's main view (it's parent, I assume) and be as wide as the view. Try this...
CGRect viewFrame = self.view.frame;
CGRect pickerHeight = self.picker.frame.size.height; // assume you have an outlet called picker
CGRect pickerFrame = CGRectMake(0.0, viewFrame.size.height-pickerHeight,
viewFrame.size.width, pickerHeight);
self.picker.frame = pickerFrame;

Related

First UIView not autosizing to fit iPhone 5 or iPad

I have a simple app with multiple UIViews. I've recently begun work to create a universal app out of it get the auto-sizing and rotation of views working with the iPhone 5 and iPad.
On my very first view, the view comes up as the standard 3.5" display with a white bar beneath it on the iPhone 5 (and beside it on the iPad). There is also a toolbar attached to the bottom of the view and it appears at the place where it would be on a 3.5" display. The strange thing is, when I go to another view and then return to the root view, the view is sized correctly.
I've checked my autosizing for the view - all outside and inside anchors are activated. My view mode is set to "scale to fit," and in comparison to other views, settings are the same. And the strange thing is it comes up right the second time the view is displayed.
Any thoughts?
I found my problem. It was in the didFinishLaunchingWithOptions method of the application.
I changed:
UIView *rootView = self.rootViewController.view;
CGRect rootViewFrame = rootView.frame;
rootViewFrame.origin.y +=[UIApplication sharedApplication].statusBarFrame.size.height;
to
UIView *rootView = self.rootViewController.view;
CGRect rootViewFrame = [[UIScreen mainScreen] applicationFrame];
The app was picking up the hard coded size of the view from the XIB when the view first loaded; now I just fit it within the application frame.

iPhone iOS large UIView gets clipped to 320x460 when displayed. How to prevent?

I have a 850x1100 UIView defined in a storyboard it is 4 times the size of a normal window, and I'm using it as a PDF generation template.
The view is wrapped within a scrollview. Yet when I display the view, its frame gets resized and becomes 320x460, effectively cutting off everything outside the frame.
Does anyone have insight on why my UIView defined in a storyboard as 850x1100 gets its frame reset to 320x460 when displayed on an iPhone?
This frame recalculation is not a bug. The frame of a view represents its position and size relative to the screen coordinates.
The iPhone viewing area is 320x480 and any view that has its frame set to a CGRect larger than the viewing area of the device will become "clipped" and have its frame set back to the viewing area (320x460 viewing area due to the status bar).
Try:
scrollView.contentSize = CGSizeMake(850,1100);
scrollView.clipsToBounds = NO;
pdfView.clipsToBounds = NO;
Are you sure it's not the scrollview whose contentsize is too small? It won't automatically resize based on the content - you have to explicitly set the contentSize of the UIScrollView

Resizing Iphone ScrollView breaks fields

I have a form about the size of the screen height and am trying to resize the scroll view's frame when the keyboard pops up, which works just fine. Found a lot of great tutorials on that. But after the keyboard pops up I can only click the first 4 fields (coincidentally the ones above the keyboard). I can still use tab to navigate to the lower ones and click them before the keyboard pops up, but I can't scroll to a lower text field and click on it.
I'm not doing much code at all, but I can't tell why it would do this.
- (void) makeRoomForKeyboard:(NSNotification *)notif {
float keyboardHeight = [[[notif userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
scrollForm.frame = CGRectMake(0, 0, scrollForm.frame.size.width, windowHeight-keyboardHeight);
}
I put one of the fields that wasn't working higher up where the good fields are and it again worked fine. I'm guessing there's a rect or something that I'm not setting that triggers the touch event or something.
Don't change the scrollForm frame, instead set the bottom contentInset.
I think you need to resize the UIScrollView's content area in addition to adjusting the frame.
If your UIScrollView was originally the correct size to display all the content without scrolling, you could say something like this:
scrollForm.contentSize = scrollForm.frame.size;
scrollForm.frame = CGRectMake(0, 0, scrollForm.frame.size.width, windowHeight-keyboardHeight);
That should allow you to scroll to the area of the scroll view covered by the keyboard.

Cannot properly rotate a translated UIView on the iPad

I have an UIViewController that has two UIViews inside, whose layout (set in Interface Builder) is something like this (the magenta colored view is the UIViewController default view):
and I added the main view to a root view obtaining this effect (by not showing the darker view placing its frame at the bottom):
Now when the device rotates, what I obtain is this:
whereas I'd like to still hide the darker view.
This is the code I use to hide it when it appears (I use it in the viewDidLoad method):
- (void)hideAll:(float)time{
CGRect hiddenFrame = CGRectMake(0, 1024, self.view.frame.size.width, self.view.frame.size.height);
[self.view setFrame:hiddenFrame];
[self.view setNeedsDisplay];
}
and it appears to work, but when I call a variant of it when rotating (in willRotateToInterfaceOrientation:duration:), nothing happens:
CGRect hiddenFrame = CGRectMake(0, 748, self.view.frame.size.width, self.view.frame.size.height);
[self.view setFrame:hiddenFrame];
[self.view setNeedsDisplay];
What am I missing? have I to set some other view property in Interface Builder?
UPDATE
It seems to me that the coordinate system does not change after the device has been rotated (ie the origin set in the upper-left corner in portrait mode will become the upper-right after a clockwise rotation in landscape mode)
Check your struts and springs in IB. It maybe that when you autorotate, the strut fixing you to the top of the screen moves your frame back to the top.
Try adding your code to the didRotateToInterfaceOrientation method so it can run after rotation has occurred.
Why to you change the frame to hide it, and not just
[self.view setHidden:YES];
second thing is that you should try to set the "Autoresizing" panel to handle the orientation changes, and not by changing the frame of the view.
hope i understood you right.
shani
The problem I encountered can be faced in two ways (I hope this can save some time for someone in the future):
using an UIViewcontroller (loaded from xib) and only its view as the rotating view, that is my original attempt to solve it. In this case you have to programmatically set the frame for the view when you want to hide it, and by doing so you have to consider that the coordinate system for the controller remains untouched and does not rotate (ie. it is always originated from the same corner)
using a UIView (loaded from xib) inside an already existing UIViewcontroller, in this way the view will automatically rotate and resize and you will not have to take the proper coordinates value into account at each rotation (this is by far the simplest way)

ipad iphone repositioning uinavigation bar, moving it down to have a logo

Does anyone know if the uinvagitionbar of a uinavigationcontroller can be moved down? I'd like to move it around 200 pixels down to have a logo on top. From my research, I understand that the navigationbar should not be subclassed and there are only two properties that should be changed, it's color and it's visibility. So is this impossible?
I tried moving it's frame, but to no avail.
I've seen other apps do it, but I'm thinking it might be a toolbar? Can the toolbar be repositioned?
Thanks
Just change the size of the frame of the navigations controller's view.
CGRect frame = navigationController.view.frame;
frame.size.height -= 200.0f;
frame.origin.y += 200.0f;
navigationController.view.frame = frame;
You can then add whatever view you'd like to the view or window that contains the navigationController's view.
There a few tricky things to consider if you plan on doing this by presenting a modal view controller, however.
You can hide the self.navigationController and put another navigation bar in the code which will move according to your frame set.
Yes the tool bar can be repositioned. Just take a control in your code and set its frame as per your requirement.
Happy Coding...