0

I want to show some information annotated on a map and below the map I want to show a very simple custom date picker.

I created a custom view in a nib and associated it with an instance of UIView.

This same class contains this function to load the view instance from a nib.

+(id)loadCustomDatePicker
{
    CustomDatePicker *customDatePicker = [[[NSBundle mainBundle] loadNibNamed:@"CustomDatePicker" owner:nil options:nil] lastObject];

    if ([customDatePicker isKindOfClass:[CustomDatePicker class]])
        return customDatePicker;
    else
        return nil;
}

Here is my screenshot from interface builder.

enter image description here

In my view controller's viewDidAppear method, I am using this code to load the custom view

CustomDatePicker *customDatePicker = [CustomDatePicker loadCustomDatePicker];

[self.view addSubview:customDatePicker];
[self.view bringSubviewToFront:customDatePicker];

customDatePicker.frame = CGRectMake(0, 375, 320, 40);

Here is the simulation screenshot:

enter image description here

Dimensions of the custom view have been set to 320 x 40 in the nib file. Background is transparent.

HERE IS THE PROBLEM:

Since height of the scene is 504, my code should have been

 customDatePicker.frame = CGRectMake(0, 464, 320, 40);

But when I implement this I can only see the map. Even (0,400), (0,420) does not work. I used trial and error and used (0,375).

Visually my objective seems to be complete. But I dont know why (0,375) is working when it is not supposed to work.

Shuaib
  • 779
  • 2
  • 13
  • 47

1 Answers1

1

It depends on which view 'self.view' is. It looks like that view is the map only (not including the search bar). Why don't you set

customDatePicker.frame = CGRectMake(0, self.view.size.height - 40, 320, 40)

then you are not dependent on the actual size of the view (which will differ between iPhone 4 and 5 anyways).

BTW, viewDidAppear is a strange method to add the bar, viewDidLoad is intended for that. Add it in viewDidLoad, then set its frame in viewViewAppear.

fishinear
  • 6,101
  • 3
  • 36
  • 84
  • Thanks, CGRectMake(0, self.view.size.height - 40, 320, 40) works! However if I put the datepicker initialization code in viewDidLoad it does not work. I dont know why. – Shuaib Jul 30 '13 at 03:58
  • Sorry, yes, in viewDidLoad the view frames have not been set correctly yet. I have updated my answer. – fishinear Jul 30 '13 at 10:41