Search code examples
objective-cipaduipickerview

how to change the width of the UIPickerView in objective C


how to change the width of the UIPickerView in objective C, I am using the following code,

    tempFiled = Data;
    [tempFiled resignFirstResponder];
    CGSize sizeOfPopover = CGSizeMake(200, 200);
    CGPoint positionOfPopover = CGPointMake(32, 325);
    [popOverControllerWithPicker presentPopoverFromRect:CGRectMake(positionOfPopover.x, positionOfPopover.y+10, 500, sizeOfPopover.height)
         inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

But when i am trying to change the width in CGSize sizeOfPopover = CGSizeMake(200, 200); its not changing, i want to reduce the size of the picker.


Solution

  • I had this problem and solved it this way:

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        CGRect frame = bpsPicker.frame;
        frame.size.width = 100; // width to be displayed on popover controller
        bpsPicker.frame = frame;
    
    }
    

    Using ViewDidLoad() is too early to alter this value.

    To be clear the viewWillAppear is in the UIViewController that is passed to the UIPopover (here called 'MyViewController.m').So,

    self.myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] ;
    self.myViewController.delegate = self;
    self.myViewController.contentSizeForViewInPopover = CGSizeMake(320, 360); // or whatever
    
    self.myViewControllerPopover = [[UIPopoverController alloc] initWithContentViewController:self.myViewController] ; 
    
    [self.myViewControllerPopover presentPopoverFromBarButtonItem:self.myToolBarButton permittedArrowDirections:UIPopoverArrowDirectionAny  animated:YES];