Don_Mag

Picker view example

Mar 4th, 2022 (edited)
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface PickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
  4.  
  5. @end
  6.  
  7. @interface PickerViewController ()
  8.  
  9. {
  10.     NSArray *letterArray;
  11. }
  12.  
  13. @property (strong, nonatomic) UIView *viewForMainPicker;
  14.  
  15. @end
  16.  
  17. @implementation PickerViewController
  18.  
  19. - (void)viewDidLoad {
  20.     [super viewDidLoad];
  21.     self.tabBarController.tabBar.hidden = YES;
  22.     //[self.view setBackgroundColor:[UIColor clearColor]];
  23.     [self.view setBackgroundColor:[UIColor systemYellowColor]];
  24.    
  25.     letterArray = @[@"",@"A",@"B",@"C",@"D"];
  26.    
  27.     UIBarButtonItem *pBtn = [[UIBarButtonItem alloc] initWithTitle:@"picker"
  28.                                                              style:UIBarButtonItemStylePlain
  29.                                                             target:self
  30.                                                             action:@selector(launchPicker:)];
  31.     self.navigationItem.rightBarButtonItem = pBtn;
  32. }
  33.  
  34. - (void)launchPicker:(id)sender {
  35.    
  36.     // let's use auto-layout / constraints instead of explicit frames
  37.     //self.viewForMainPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 400, [[UIScreen mainScreen] bounds].size.width, 220)];
  38.     //UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 44)];
  39.     //UIPickerView *mainPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 220)];
  40.    
  41.     self.viewForMainPicker = [UIView new];
  42.     UIToolbar *toolBar = [UIToolbar new];
  43.     UIPickerView *mainPicker = [UIPickerView new];
  44.    
  45.     UIFont *arrowfont = [UIFont boldSystemFontOfSize:40];
  46.     UIFont *titlefont = [UIFont boldSystemFontOfSize:28];
  47.     UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"→"
  48.                                                                 style:UIBarButtonItemStylePlain
  49.                                                                target:self
  50.                                                                action:@selector(fakeEvent)];
  51.     UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:@"Select Gender"
  52.                                                               style:UIBarButtonItemStylePlain
  53.                                                              target:nil
  54.                                                              action:nil];
  55.     UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
  56.                                                                           target:self
  57.                                                                           action:nil];
  58.     [doneBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
  59.                                      [UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
  60.                                      arrowfont, NSFontAttributeName, nil] forState:UIControlStateNormal];
  61.     [title setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
  62.                                    [UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
  63.                                    titlefont, NSFontAttributeName, nil] forState:UIControlStateNormal];
  64.     NSArray *btnArray = [[NSArray alloc] initWithObjects:flex,flex,title,flex,doneBtn,nil];
  65.    
  66.     mainPicker.delegate = self;
  67.     mainPicker.dataSource = self;
  68.    
  69.     [toolBar setItems:btnArray];
  70.     [_viewForMainPicker addSubview:mainPicker];
  71.     [_viewForMainPicker addSubview:toolBar];
  72.    
  73.     [self.view addSubview:_viewForMainPicker];
  74.    
  75.     _viewForMainPicker.translatesAutoresizingMaskIntoConstraints = NO;
  76.     mainPicker.translatesAutoresizingMaskIntoConstraints = NO;
  77.     toolBar.translatesAutoresizingMaskIntoConstraints = NO;
  78.    
  79.     UILayoutGuide *g = self.view.safeAreaLayoutGuide;
  80.    
  81.     [NSLayoutConstraint activateConstraints:@[
  82.        
  83.         [_viewForMainPicker.topAnchor constraintEqualToAnchor:g.topAnchor constant:400.0],
  84.         [_viewForMainPicker.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:0.0],
  85.         [_viewForMainPicker.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:0.0],
  86.         [_viewForMainPicker.heightAnchor constraintEqualToConstant:220.0],
  87.  
  88.         [toolBar.topAnchor constraintEqualToAnchor:_viewForMainPicker.topAnchor constant:0.0],
  89.         [toolBar.leadingAnchor constraintEqualToAnchor:_viewForMainPicker.leadingAnchor constant:0.0],
  90.         [toolBar.trailingAnchor constraintEqualToAnchor:_viewForMainPicker.trailingAnchor constant:0.0],
  91.         [toolBar.heightAnchor constraintEqualToConstant:44.0],
  92.  
  93.         [mainPicker.topAnchor constraintEqualToAnchor:toolBar.bottomAnchor constant:0.0],
  94.         [mainPicker.leadingAnchor constraintEqualToAnchor:_viewForMainPicker.leadingAnchor constant:0.0],
  95.         [mainPicker.trailingAnchor constraintEqualToAnchor:_viewForMainPicker.trailingAnchor constant:0.0],
  96.         [mainPicker.bottomAnchor constraintEqualToAnchor:_viewForMainPicker.bottomAnchor constant:0.0],
  97.  
  98.     ]];
  99.    
  100.     self.viewForMainPicker.backgroundColor = [UIColor whiteColor];
  101.    
  102. }
  103.  
  104. - (void)fakeEvent {
  105.     NSLog(@"selected");
  106. }
  107.  
  108. - (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
  109.     return 1;
  110. }
  111.  
  112. - (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  113.     return letterArray.count;
  114. }
  115.  
  116. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  117.     return letterArray[row];
  118. }
  119.  
  120. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  121.     NSString *newString = [NSString stringWithFormat:@"%@", letterArray[row]];
  122.     NSLog(@"passing: %@",newString);
  123. }
  124.  
  125. @end
  126.  
Add Comment
Please, Sign In to add comment