0

I have a UIDatepicker where i have added border image so that it sits on top of UIDatepicker border . In simulator the border image is in exactly on top of UIDatepicker border ,but when i run the project on iphone / ipod device .the border image tends to be in out of position .Why is this happening ?

When i tap on settingsButton ..settingsButtonChanged method is called and in settingsView datepicker is added .

Thanks

UPDATE :

 -(void)viewDidLoad
    {
        userTimePicker = [[UIDatePicker alloc]init

    }


    -(IBAction)settingsButtonChanged:(UIButton *)sender
    {


        UIImageView *settingsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"settingsViewImage.png"]];
        settingsImage.frame = CGRectMake(0.0, 0.0, 280.0, 370.0);

        CGFloat height = [UIScreen mainScreen].bounds.size.height;

        if(height==568.00)
        {
            settingsView.frame = CGRectMake(0.0, 50.0, 280.0, 370.0);

        }else
        {
            settingsView.frame = CGRectMake(20.0, 45.0, 280.0, 370.0);
        }

        settingsView.backgroundColor = [UIColor clearColor];
        [settingsView addSubview:settingsImage];


        UIImageView *userTimePickerBorder = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"datepickerBorder.png"]];
        userTimePickerBorder.frame = CGRectMake(0.0, 0.0, 150.0, 180.0);

        userTimePicker.frame = CGRectMake(65.0, 165.0, 150.0, 180.0);
        userTimePicker.datePickerMode = UIDatePickerModeTime;

        [settingsView addSubview:userTimePicker];
        [userTimePicker addSubview:userTimePickerBorder];

        [symptomView addSubview:settingsView];
    }
raptor
  • 291
  • 1
  • 9
  • 17
  • 1
    Could you separate this into two different questions? Thanks! – matt May 01 '13 at 16:20
  • Simulator differing from device is really strange. Are you remembering to set the simulator to the same type of device? If you do, does the problem happen then? I'm thinking this might be a constraints/autoresizing problem. - It might help if you posted your code that places the border image. Or screen shots. Or both! :) – matt May 01 '13 at 16:38
  • @matt u have said set the simulator to the same type of device u mean if i have non retina display iphone and using non retina display simulator ..I am adding the code i have added 2nd question in stackoverflow – raptor May 01 '13 at 16:49

1 Answers1

1

Suggestion 1

Well, first of all, this code makes no sense at all:

- (void)viewDidLoad {
    userTimePicker = [[UIDatePicker alloc]init];
}

Consider what that does. Suppose you have a property or instance variable called userTimePicker (you must have something like that, right?). Now, either it is an outlet pointing to an actual date picker coming from the nib/storyboard, or it isn't. Well then:

  • If it is, now it isn't! You've just overwritten the reference to the actual date picker in the interface with a different date picker.

  • If it isn't, you've just set userTimePicker to a date picker, but that date picker is not in the interface (you have no code adding it to the interface).

So, either way, from now on, userTimePicker is useless; it does not point to anything in the interface.

So you would certainly need to fix that before doing anything else!

Suggestion 2

Also, I have a suggestion for why your results on the simulator differ from your results on the device: it might be because you've been testing repeatedly on the simulator. This can cause old code/resources to be present in the simulator version of your app. To fix that, clean out your caches and restore the simulator to its defaults, as I describe here: https://stackoverflow.com/a/6247073/341994 I'm hoping that will at least cause the simulator and the device to behave the same! And then you can get on with the real business of fixing your code.

Suggestion 3

This code is really weird:

    CGFloat height = [UIScreen mainScreen].bounds.size.height;

    if(height==568.00)
    {
        settingsView.frame = CGRectMake(0.0, 50.0, 280.0, 370.0);

    }else
    {
        settingsView.frame = CGRectMake(20.0, 45.0, 280.0, 370.0);
    }

You should not be consulting the screen bounds for anything! All of this should be taking place within some view controller. The view controller's view should rotate and resize to fit the device orientation or screen size, so the bounds of the view controller's view will change, and that is what you should should be checking.

And instead of hard-coding those frame values, you should express them in terms of the view controller view's bounds, or the bounds of the superview they are to go into. That will give you consistent results. Even better, if this is on iOS 6, use constraints instead of setting frames.

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • The userTimePicker which is UIDatepicker is not set in xib file . Shouldnt i alloc init if i havent added in xib file – raptor May 01 '13 at 17:24
  • Yes, but I got thrown for a loop by the fact that you didn't also add it to the interface at the same time. Instead, you are adding it to the interface every time `settingsButtonChanged:` is called! Is that really what you intend? – matt May 01 '13 at 17:26
  • Yes i have been testing the app in simulator more than the device and you might be right it might have cached the old code – raptor May 01 '13 at 17:26
  • i am adding datepicker only when `settingsButtonChanged:` method ..this method will open settingsView where datepicker is present – raptor May 01 '13 at 17:29
  • Okay, well disregard what I said about the alloc-init then. But really you could alloc-init together with the stuff in `settingsButtonChanged:` if that is the first place where you need this date picker. – matt May 01 '13 at 17:30
  • okay ..i didnt add this line code where when datepicker stops at certain point i am calling a method where i am displaying the time shown in datepicker on uilabel ..can i call that method in settingsButtonchanged method after i alloc init datepicker – raptor May 01 '13 at 17:35
  • The sugesstion 3 code is used ..so that the image fits for iphone 4/4s and iphone 5 devices...those images are different i wouldnt worry about that...thats not datepicker border image – raptor May 01 '13 at 17:42