Default UIPickerView height - iphone

How can I programatically obtain the default height of an UIPickerView instance, in accordance to the resolution and orientation of the device that the app is currently running on?
I would like not to use a hardcoded value for this parameter, in the event that new devices will support different screen resolutions and thus will determine this component to have a different default size.

The warning that André saw doesn't seem to happen anymore (Xcode 4.2; iOS SDK 5.0). If you create it with initWithFrame and give it a height of zero, you can then read back it's height from the frame.height property:
uiPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 77, 320, 0)];
DLog(#"uiPickerView=%#",uiPickerView);
gives:
uiPickerView=<UIPickerView: 0x9a28c40; frame = (0 77; 320 216); layer = <CALayer: 0x9a28d00>>
So 216 is the default height.
I couldn't find a definition for this in the headers, so reading it back seems to be the safest way.
It is possible to set a frame with a non-zero height, but Apple say don't do that, and it seems to cause rendering problems.

With auto layout I was wondering the same due to the fact that you can't pin to the bottom of it.
The default height with no changes to an UIPickerView is 216px

If you create an instance with a height of 0, it will be overridden with the appropriate default height, and you can just get its frame.size.height.
However, on iPad this will issue a warning (tested a few days ago). I eventually had to use hardcoded values for iPad...
P.S. This was for a UIDatePicker; not sure if it's the exact same thing for UIPickerView.

In 2022 it is 162. Pic is not touching the height just pulling into IB

You can use the frame property of the uipickerview.

Related

resizableImageWithCapInsets issue in iOS7

I am following MessagesTableViewController and there is method of starching of UIImage for bubble view to strech as per text size. It is working fine with older ios version but in ios7, it is displaying light color borders as we set UIEdgeInsetsMake as below.
+ (UIImage *)bubbleDefaultIncoming
{
return [[UIImage imageNamed:#"bg-chat-white.png"] makeStretchableDefaultIncoming];
}
- (UIImage *)makeStretchableDefaultIncoming
{
return [self resizableImageWithCapInsets:UIEdgeInsetsMake(15.0f,20.0f, 15.0f, 20.0f)
resizingMode:UIImageResizingModeStretch];
}
Here i attached 2 snapshot for ios6 and ios7 which describe how bubble View is behaving strange with ios7 though code is same.
and
Someone has also same issue and reported in GITHUB HERE
I reviewed code so much and it seems that there is issue with resizableImageWithCapInsets in ios7. It generates borders as we set UIEdgeInsetsMakein the method.
Anyone has idea or solution to remove the borders from bubble view and make same as ios6 bubble view?
Any help would be appreciable. Thanks in advance.
Transparent lines are added in iOS 7 when the width or height is a number with floating point. As a workaround, you can round this numbers
You need to ensure that the CGRect you are drawing the image into is an even number and not a number with a floating point.
In addition to this if you have a UITableView with TabViewCells that have different heights you also need to need to ensure those cells all have heights that are an even number and not a number with a floating point.
i can confirm that both answers are right, but since you are using the same framework as me i going to give you a snippet to help.
just floor or ceil the size of the bubble and you are good to go.
- (CGRect)bubbleFrame
{
CGSize bubbleSize = [JSBubbleView bubbleSizeForText:self.text];
return CGRectMake((self.type == JSBubbleMessageTypeOutgoing ? floor(self.frame.size.width - bubbleSize.width) : 0),
kMarginTop,
floor(bubbleSize.width),
floor(bubbleSize.height));
}
edit: the position too need to be rounded up or down, since the kMarginTop already is you only need for it when it's a outgoing bubble. peace

Button with background image - iPhone 5 resize issue

I have been looking everywhere on here and Google, but I still have this weird issue that I can't figure. I have an app pictures below, with 4 UIButtons that have a background image applied:
When I resize/run on the iPhone 5, the top button resizes in a very strange way:
How do I get all the UIButtons to resize the same way with the iPhone 5? Is it possible? Do I need to use a different method?
I really hope this is not a duplicate, but I am completely stumped. If I change the UIButtons around in other positions along the UIView, a different one will resize, or strange gap spacing will show up.... I don't understand. Thanks for any help!
EDIT: I am using iOS 6 on Xcode 4.5. I am testing on an iPhone 4S and an iPhone 5. I have AutoLayout checked. If I use Pin (just found it) I can get the UIButton to keep its height.
To clarify my question. I want the UIButtons to resize so they occupy the same percentage of the screen.... I want the proportions to be the same, instead of simply adding a bunch of empty space. I hope that make it more clear
One of the features you can use is called AutoLayout. Its provided with Xcode to make it easier adjusting things for the 4 inch screen. You can find a good example here: http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2
But as far as I know AutoLayout works only with iOS 6 which makes it "not so useful yet". I have been using a class called UIDevice, source of which can be found here: https://github.com/erica/uidevice-extension and check if the platform type is iPhone 5. I have it set up in a method which setups GUI when the view loads. Example of my implementation:
UIImage *backgroundImage;
if ([[UIDevice currentDevice] platformType] == UIDevice5iPhone)
{
//I have a 4 inch screen present
myButton.frame = CGRectMake(x, y, w, h); //Set the frame as you would like it to look on the iPhone 5
backgroundImage = [[UIImage alloc] initWithCGImage:[UIImage imageNamed:#"main_background-568h#2x"].CGImage scale:2.0 orientation:UIImageOrientationUp];
}
else
{
//I have a 3.5 inch screen present
myButton.frame = CGRectMake(x, y, w, h); //Set the frame as you would like it to look on the iPhone 3, 3gs, 4, 4s...
backgroundImage = [[UIImage imageNamed:#"main_background"] retain];
}
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
[backgroundImage release];
Hope it clears a bit.
you should firstly disable the use AutoLayout this option is under the "Show the file inspector"

iphone and iPad difference in Designing

I am asking a very basic question in which I am getting some problem.
I know the difference between the iPhone and iPad ...that iPad supports SplitView, popup over but while designing I am getting error.
I have designd a UITextView
UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(42, 150, 440, 60)];
this is basic code to design a textview but when I run this code on iPad simulator it seems fine to me. but When I run it on iPhone ,It does not goes well ..because size for that iPhone is different .In that case what should I do to run it well on both iPhone and iPad.
I am assuming that you are opting for a universal executable, so conditional compilation is not an option for you.
When you make a universal executable, you should check the features that you are relying upon before making calls dependent on the device type. In this particular case you are relying upon the screen having a particular size. Instead of hard-coding the "magic numbers" (42, 15, 440, and 60) you should calculate them from the current size of the available screen:
CGFloat w = [UIScreen mainScreen].bounds.size.width;
CGFloat h = [UIScreen mainScreen].bounds.size.height;
// Do something like this if you can
UITextView *textview = [[UITextView alloc]
initWithFrame:CGRectMake(w*0.025, h*0.25, w*0.5, h*0.125)
];
There is a chance that calculating actual sizes from the screen size is not possible, because you do not want your view to scale proportionally to the screen. In cases like that you can check the values of h and w, detect the device size, and use the corresponding set of pre-defined sizes to init your view.

How do I use CGRectMake to size the background

Ok So I have this code, which allows me to put a background image in:
I would love to know how to size this, so on the iPhone 4 I can get a 320x480 size but make it nice with an image of 570.855.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background_stream-570x855.jpg"]];
I have tried this:
UIImageView *image = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"background_stream-570x855.jpg"]];
[self.view sendSubviewToBack:streamBG];
image.frame = CGRectMake(0, 0, 320, 480);
Which works, however the problem is it's behind the view, so I can't see it. I could make the view clear, but it has objects on it that need to be displayed.
Any help would be most apretiated
There are multiple options to put Views at desired location.
[self.view sendSubviewToBack:streamBG]; //Sends subview to last position i.e. at the last
[self.view bringSubviewToFront:streamBG] //Brings subview to first position i.e. at the first.
[self.view insertSubview:streamBG atIndex:yourDesiredIndex];//Put at desired index.
This is not answer to your question, though it may help you to set your imageview to desired location.
Hope it helps.
To answer part of your question about sizing. You need to use 2 different images for your app if you want the full resolution of the retina display (iPhone4) You should provide a 320x480 image (i.e. myImage.png) for older iPhones and one at twice the resolution, 640x960, for the iPhone 4. Use the exact same name for the high res version but add an "#2x" to the end (i.e. myImage#2x.png). In your code all you ever have to call is the base name (i.e. myImage.png) the app will choose the correct image based on the hardware its running on. That will get you the best experience on your app.
On the other part about the background view visibility, you could make the background color clear ([UIColor clearColor]) on the view that is blocking it. That would leave the content visible in that view but the view its self would be clear. Alternatively you could just insert the background at a specific index as #Jennis has suggested instead of forcing it to the back.

Width of viewController is 320px when iPhone is landscape

I have a viewController and I have
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
why I read w and h as 320 and 480 on its viewDidLoad for width and height, instead of 480 and 320, respectively, when iPhone is landscape? (see below)
- (void) viewDidLoad {
CGFloat w = self.view.bounds.size.width; // 320???
CGFloat h = self.view.bounds.size.height; // 480 ??
}
What am I missing?
thanks.
Be careful, it is possible you have run in to perhaps the most famous bug on the platform:
iPhone app in landscape mode, 2008 systems
This is a bug that catches a lot of people. Notice that on that question, one of the responses has 22 votes - that response is actually completely, totally, 100% wrong.
That shows some of the confusion on the issue.
Be sure to check out everything on there and all the links, eg:
"An important reminder of the ADDITIONAL well-known problem at hand here: if you are trying to swap between MORE THAN ONE view (all landscape), IT SIMPLY DOES NOT WORK. It is essential to remember this or you will waste days on the problem. It is literally NOT POSSIBLE. It is the biggest open, known, bug on the iOS platform."
Did you try to obtain the values in viewDidAppear? With viewDidLoad the view was not actually rendered and so it might be still in portrait.
I always have this problem...
I think that all views must render in portrait mode and then switch to landscape. Its a bitch, but the best way to manage this is to use UIViewAutoreizingMasks on all sub-views, that way they'll layout how you want them to.