Reputation: 291
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];
}
Upvotes: 0
Views: 157
Reputation: 535954
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!
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.
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.
Upvotes: 1