#import <UIKit/UIKit.h>
@interface PickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
@end
@interface PickerViewController ()
{
NSArray *letterArray;
}
@property (strong, nonatomic) UIView *viewForMainPicker;
@end
@implementation PickerViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tabBarController.tabBar.hidden = YES;
//[self.view setBackgroundColor:[UIColor clearColor]];
[self.view setBackgroundColor:[UIColor systemYellowColor]];
letterArray = @[@"",@"A",@"B",@"C",@"D"];
UIBarButtonItem *pBtn = [[UIBarButtonItem alloc] initWithTitle:@"picker"
style:UIBarButtonItemStylePlain
target:self
action:@selector(launchPicker:)];
self.navigationItem.rightBarButtonItem = pBtn;
}
- (void)launchPicker:(id)sender {
// let's use auto-layout / constraints instead of explicit frames
//self.viewForMainPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 400, [[UIScreen mainScreen] bounds].size.width, 220)];
//UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 44)];
//UIPickerView *mainPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 220)];
self.viewForMainPicker = [UIView new];
UIToolbar *toolBar = [UIToolbar new];
UIPickerView *mainPicker = [UIPickerView new];
UIFont *arrowfont = [UIFont boldSystemFontOfSize:40];
UIFont *titlefont = [UIFont boldSystemFontOfSize:28];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"→"
style:UIBarButtonItemStylePlain
target:self
action:@selector(fakeEvent)];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:@"Select Gender"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
[doneBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
arrowfont, NSFontAttributeName, nil] forState:UIControlStateNormal];
[title setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
titlefont, NSFontAttributeName, nil] forState:UIControlStateNormal];
NSArray *btnArray = [[NSArray alloc] initWithObjects:flex,flex,title,flex,doneBtn,nil];
mainPicker.delegate = self;
mainPicker.dataSource = self;
[toolBar setItems:btnArray];
[_viewForMainPicker addSubview:mainPicker];
[_viewForMainPicker addSubview:toolBar];
[self.view addSubview:_viewForMainPicker];
_viewForMainPicker.translatesAutoresizingMaskIntoConstraints = NO;
mainPicker.translatesAutoresizingMaskIntoConstraints = NO;
toolBar.translatesAutoresizingMaskIntoConstraints = NO;
UILayoutGuide *g = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[_viewForMainPicker.topAnchor constraintEqualToAnchor:g.topAnchor constant:400.0],
[_viewForMainPicker.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:0.0],
[_viewForMainPicker.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:0.0],
[_viewForMainPicker.heightAnchor constraintEqualToConstant:220.0],
[toolBar.topAnchor constraintEqualToAnchor:_viewForMainPicker.topAnchor constant:0.0],
[toolBar.leadingAnchor constraintEqualToAnchor:_viewForMainPicker.leadingAnchor constant:0.0],
[toolBar.trailingAnchor constraintEqualToAnchor:_viewForMainPicker.trailingAnchor constant:0.0],
[toolBar.heightAnchor constraintEqualToConstant:44.0],
[mainPicker.topAnchor constraintEqualToAnchor:toolBar.bottomAnchor constant:0.0],
[mainPicker.leadingAnchor constraintEqualToAnchor:_viewForMainPicker.leadingAnchor constant:0.0],
[mainPicker.trailingAnchor constraintEqualToAnchor:_viewForMainPicker.trailingAnchor constant:0.0],
[mainPicker.bottomAnchor constraintEqualToAnchor:_viewForMainPicker.bottomAnchor constant:0.0],
]];
self.viewForMainPicker.backgroundColor = [UIColor whiteColor];
}
- (void)fakeEvent {
NSLog(@"selected");
}
- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return letterArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return letterArray[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *newString = [NSString stringWithFormat:@"%@", letterArray[row]];
NSLog(@"passing: %@",newString);
}
@end
Comments
0 B
|👍
/👎