1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

PickerView in miss location after rotated

Discussion in 'iPhone/iPad Programming' started by mikezang, Oct 16, 2010.

  1. macrumors 6502a

    I use code as below to show a PickerView, but is not located at middle of center, then if I rotated, the PickerView is not to rotated, what should I have to implement for tight showing?
    - (void)showPickerView {
        if (pickerView == nil) {
            pickerView = [[UIDatePicker alloc] initWithFrame: CGRectMake(0.0, 0.0, 0.0, 0.0)];
        if (self.pickerView.superview == nil) {
            [self.view.window addSubview: self.pickerView];
            // size up the picker view to our screen and compute the start/end frame origin 
            // for our slide up animation compute the start frame
            CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
            CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
            CGRect startRect = CGRectMake(0.0, screenRect.origin.y + screenRect.size.height, pickerSize.width, pickerSize.height);
            self.pickerView.frame = startRect;
            // compute the end frame
            CGRect pickerRect = CGRectMake(0.0, screenRect.origin.y + screenRect.size.height - pickerSize.height, pickerSize.width, pickerSize.height);
            // start the slide up animation
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
            // we need to perform some post operations after the animation is complete
            [UIView setAnimationDelegate:self];
            self.pickerView.frame = pickerRect;
            // shrink the table vertical size to make room for the date picker
            CGRect newFrame = detail.frame;
            newFrame.size.height -= self.pickerView.frame.size.height;
            self.detail.frame = newFrame;
            [UIView commitAnimations];

    Attached Files:

  2. macrumors 6502a

    I knew why PickerView is not at the center of screen, because code as below return a mistake size. I run in Portrait mode, but system gives me a Landscape width. Is this a bug in SDK?
    CGSize pickerSize = [pickerView sizeThatFits:CGSizeZero];

    Attached Files:

  3. macrumors 6502

    Might be. How does the code behave on the hardware? Have you tried the iOS 4.2 beta?

    Regarding centre positioning, set screenRect.size.width as the width in the startRect and pickerRect. (UIDatePicker centres itself within the given rectangle.)
  4. macrumors 6502a

    I used code as below to correct miss width. Only in 4.1.
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    CGSize pickerSize = [pickerView sizeThatFits:CGSizeZero];
    pickerSize.width = screenRect.size.width;	
    By the way, do you have any idea for rotation problem?
  5. macrumors 6502

    Have you tried to use a popover? I think that is the natural human-interaction paradigm with the iPad. (I don't have an iPad.)
  6. macrumors demi-god

    Which version of the SDK is this? ie. Why is there a window around the simulator?
  7. macrumors 6502a

    You have special eyes.

    I am using iOS SDK 4.1 and do my job by VNC from a ThinkPad to Mac mini.

    Can you tell me where I can find a sample for PickerView in Popover?
  8. Moderator


    Staff Member

    By this point I would hope you would be able to code your own sample for such a fairly trivial need.
  9. macrumors 6502a

  10. Moderator


    Staff Member

    Absolutely. The fact you even question this shows that copying "sample code" and saying to yourself "I understand that" has taught you somewhere between absolutely nothing and very little. Perhaps you should accept that this approach is not working and actually sit down with a book and learn.
  11. macrumors 6502a

    Your suggestion is no any suggestible all time!

    You are not the Demi-God, but the God!

    If anyone can do programming just read book, who needs this forum?
  12. macrumors 6502

    You have probably seen in the documentation that UIPopoverController is initialized with a view controller whose job is to provide the content.

    Are you familiar with views and view-controllers?
  13. macrumors 6502a

    A little, though not enough. Here is found by me at first...

    But I want to use a DatePicker in popover and in programming, it is not a controller so that a UIViewController might be neede.

    I want to know if there is simpler way to use a DatePicker in popover, it seems like there is no such story on Internet...
  14. macrumors 6502

    The documentation clearly states that: UIPopoverController is initialized with a view controller whose job is to provide the content.

    There is no simpler way: you must create a hierarchy where there is a UIViewController which owns the UIDatePicker.
  15. Moderator


    Staff Member

    I thought really. I guess I was wrong.

    Most of the books we talk about around here are meant to teach you the fundamentals (and not always all of them). It is then up to you, the programmer, to use your creativity and ingenuity to solve your own problems based on your knowledge of that foundation. That, to me, is where the forum comes in: when you need help for something that's beyond the basics or when writing code that applies the fundamentals but in a way unique to your specific application. Or occasionally when a fundamental confuses you and would like it explained in a different manner.

    I would take this as your cue then to go learn enough about how views and viewcontrollers work.

    Also, remember that an important skill (probably the most important) of programming is being able to take a larger problem and divide it into smaller problems, sometimes called "divide-and-conquer", which you solve individually and then piece together.

    So, for example, for your recent problem, I would break it down into:
    1) how do I add a date picker to a view that has a viewcontroller
    2) how do I take a view, any view, that has a viewcontroller and present it from within a popover controller

    Then, it's just a matter of combining 1) and 2). Not always is this an easy thing but in this case, I think it's fairly trivial.

    Hope that helps.
  16. macrumors 6502a

    Thanks for your suggestion.

Share This Page