How to get Custom UIPickerView? - ios

I want to achieve the following functionality in Custom UIPiker as shown in below picture.
I want to change the text colour of selected area only like above.

Add lable in your pickerview in your viewDidLoad method as below.
Define label and myPickerView both in ViewController.h file
- (void)viewDidLoad
{
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
label = [[UILabel alloc] initWithFrame:CGRectMake(145, 76, 36, 36)];
//label.text = #"Label";
label.font = [UIFont boldSystemFontOfSize:20];
label.layer.cornerRadius = 18;
[label setTextColor:[UIColor whiteColor]];
label.backgroundColor = [UIColor blueColor];
label.textAlignment = NSTextAlignmentCenter;
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[myPickerView addSubview:label];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
in pickerview delegate set label title.
#pragma mark - PickerView delegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
[label setText:[NSString stringWithFormat:#"%d",row]];
NSLog(#"%#",[NSString stringWithFormat:#"%d",row]);
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSLog(#"%#",[NSString stringWithFormat:#"%d",row]);
[label setText:[NSString stringWithFormat:#"%d",row]];
return [NSString stringWithFormat:#"%d",row];
}
Your OuytPut is :

Related

How to Show the Pickerview value in Textfield and hide the pickerView using barbuttonitem?

I am Having 2 Textfields stateText and providerText.If i click the first textfield means,it shows the pickerView.After That i will Select the pickerView Value.Also i have added the toolbar barbuttonitem in that pickerView.Then I have to show the pickerView value in the Textfields and hide the pickerView after selecting the value in the PickerView.Also Pointer should moves to next TextField connectionNoText after getting Values in TextFields.
1.How to hide the PickerView and show the value in TextField?
2.How to show the pickerview in the Bottom of the View irrespective of phone size?
This is my Source Code:
NewConnectionviewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
//ScrollView
self.view.backgroundColor = [UIColor grayColor];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height + 510.0)];
[self.view addSubview:scrollView];
//statePickerView
_statePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view. frame.size.height, self.view.frame.size.width, 216)];
_statePickerView.delegate = self;
_statePickerView.dataSource = self;
_statePickerView.showsSelectionIndicator = YES;
[_statePickerView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:_statePickerView];
//providerPickerView
_providerPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view. frame.size.height, self.view.frame.size.width, 216)];
_providerPickerView.delegate = self;
_providerPickerView.showsSelectionIndicator = YES;
[_providerPickerView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:_providerPickerView];
//ToolBar
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(hidePickerView:)];
toolBar.items = #[barButtonDone];
barButtonDone.tintColor=[UIColor lightTextColor];
[barButtonDone setEnabled:YES];
[_statePickerView addSubview:toolBar];
//[_providerPickerView addSubview:toolBar];
//GettingView
[self stateView];
[self providerView];
[self connectionNoView];
[self addTestView];
}
-(void)hidePickerView:(UIBarButtonItem *)sender
{
NSLog(#"clicked");
[self.selectState resignFirstResponder];
}
-(void)stateView
{
UIView *stateView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, 80)];
stateView.backgroundColor = [UIColor blueColor];
[scrollView addSubview:stateView];
//stateLabel
UILabel *stateLabel = [[UILabel alloc]initWithFrame:CGRectMake(stateView.frame.origin.x+20,35, 70, 20)];
[stateLabel setText:#"State"];
[stateLabel setTextColor:[UIColor whiteColor]];
[stateView addSubview:stateLabel];
//TextField
NSLog(#"stateViewSize==>>%f",stateView.frame.origin.y);
stateText = [[UITextField alloc]initWithFrame:CGRectMake(stateView.frame.origin.x+110, 30, 150, 40)];
stateText.textColor = [UIColor blackColor];
stateText.backgroundColor = [UIColor clearColor];
[stateText setBorderStyle:UITextBorderStyleRoundedRect];
[stateText setPlaceholder:#"select State"];
[stateText setTag:0];
[stateView addSubview:stateText];
[stateText setDelegate:self];
}
-(void)providerView
{
UIView *providerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+80, self.view.frame.size.width, 80)];
providerView.backgroundColor = [UIColor orangeColor];
[scrollView addSubview:providerView];
NSLog(#"providerViewsize==>>%f",providerView.frame.origin.y);
//providerLabel
UILabel *providerLabel = [[UILabel alloc]initWithFrame:CGRectMake(providerView.frame.origin.x+20, 35, 70, 20)];
[providerLabel setText:#"Provider"];
[providerLabel setTextColor:[UIColor whiteColor]];
[providerView addSubview:providerLabel];
//TextField
providerText = [[UITextField alloc]initWithFrame:CGRectMake(providerView.frame.origin.x+110,30, 150, 40)];
providerText.textColor = [UIColor blackColor];
[providerText setPlaceholder:#"select Provider"];
providerText.backgroundColor = [UIColor clearColor];
[providerText setTag:1];
[providerText setBorderStyle:UITextBorderStyleRoundedRect];
[providerView addSubview:providerText];
[providerText setDelegate:self];
}
-(void)connectionNoView
{
UIView *connectionNoView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+160, self.view.frame.size.width, 150)];
connectionNoView.backgroundColor = [UIColor greenColor];
[scrollView addSubview:connectionNoView];
NSLog(#"connectionNoViewsize==>>%f",connectionNoView.frame.origin.y);
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
if (textField.tag ==0)
{
//stateText.inputView = _statePickerView;
[UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
_statePickerView.frame = CGRectMake(0, self.view.frame.size.height-216,self.view.frame.size.width, 216);
}
completion:^(BOOL finished){
}];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#""
message:#"Please select state first."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
return NO;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;//Or return whatever as you intend
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([thePickerView isEqual:_statePickerView])
{
return [STATE_ARRAY count];
}
else
{
return [PROVIDER_ARRAY count];
}
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if([thePickerView isEqual:_statePickerView])
{
return [STATE_ARRAY objectAtIndex:row];
}
else
{
return [PROVIDER_ARRAY objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *firstPickerViewValue;
NSString *secondPickerViewValue;
if ([pickerView isEqual:_statePickerView])
{
firstPickerViewValue = [STATE_ARRAY objectAtIndex:row];
[_providerPickerView selectRow:row inComponent:0 animated:YES];
secondPickerViewValue = [PROVIDER_ARRAY objectAtIndex:row];
[_providerPickerView reloadAllComponents];
[stateText setText:[NSString stringWithFormat:#"%#",firstPickerViewValue]];
[providerText setText:[NSString stringWithFormat:#"%#",secondPickerViewValue]];
}
I have read through the code and to be honest Im pretty confused by your approach as I tend to avoid magic numbers when laying out the UI. Anyway I think both your questions can be solved by assigning the inputView of your slate textField and assigning the inputAccessoryView to the toolbar you created in the code.
So for example,
stateText.inputView = _statePickerView
Well that is Swift so you would have
[stateText setInputView:_statePickerView];
Then you set the Accessory View so
[stateText setInputAccessoryView: toolBar];
Although toolbar is only in scope inside your viewDidLoad method so you should fix that. This should be enough to get you back on the right path. To dismiss the pickerView just resignFirstResponder of the selected textField inside your hidePickerView method.
Note that your textfields are also confined to the methods they are created in so you should fix that too.
Finally i Found a Solution.PickerView itself a ViewController so we can't hide the pickerView.Alternative solution is,
1.Create the SubView with Height 260 and add that View to the Self.View.
(set Y Value as Self.View.Frame.Size.height).
2.Add the PickerView(default Height - 216) and ToolBar(default Height - 44) to that SubView.
3.In BarButtonItem action Move that view to the bottom.(ex.Set Frame like this) CGRectMake(0,Self.View.Frame.Size.Height,Self.View.Frame.Size.Width,216)

UiTableViewHeader - Dynamic button event handler

I need to add a uiButton to a static uitableview section header - I've made an attempt with the following -
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// you can get the title you statically defined in the storyboard
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
CGRect frame = tableView.bounds;
// create and return a custom view
#define LABEL_PADDING 10.0f
HeaderLabelStyling *customLabel = [[HeaderLabelStyling alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] ;
UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];
addButton.backgroundColor = [UIColor whiteColor];
addButton.titleLabel.textColor = [UIColor colorWithRed:240/255.0f green:118/255.0f blue:34/255.0f alpha:1.0f];
addButton.titleLabel.tintColor = [UIColor blackColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
title.text = #"iawn";
customLabel.text = sectionTitle;
customLabel.backgroundColor= [UIColor colorWithRed:143.0f/255.0f green:137.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
customLabel.textColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
[customLabel addSubview:addButton];
[addButton addSubview:title];
[addButton addTarget:self action:#selector(receiverButtonClicked:) forControlEvents:UIControlEventTouchDown];
return customLabel;
}
-(void)receiverButtonClicked:(id)sender{
NSLog(#"button clicked");
}
the above adds a button - but doesn't react to the click event - can anyone suggest how I can get this to work?
UILabel does not handles touches by default.
Add following line of code:
customLabel.userInteractionsEnabled = YES;
In order to display button only for third section you should add following condition:
if(section == 2){...}
So your -tableView:viewForHeaderInSection: should look as follows:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection (NSInteger)section {
if(section != 2) return nil;
// you can get the title you statically defined in the storyboard
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
CGRect frame = tableView.bounds;
// create and return a custom view
#define LABEL_PADDING 10.0f
HeaderLabelStyling *customLabel = [[HeaderLabelStyling alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] ;
UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];
addButton.backgroundColor = [UIColor whiteColor];
addButton.titleLabel.textColor = [UIColor colorWithRed:240/255.0f green:118/255.0f blue:34/255.0f alpha:1.0f];
addButton.titleLabel.tintColor = [UIColor blackColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
title.text = #"iawn";
customLabel.text = sectionTitle;
customLabel.backgroundColor= [UIColor colorWithRed:143.0f/255.0f green:137.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
customLabel.userInteractionsEnabled = YES;
customLabel.textColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
[customLabel addSubview:addButton];
[addButton addSubview:title];
[addButton addTarget:self action:#selector(receiverButtonClicked:) forControlEvents:UIControlEventTouchDown];
return customLabel;
}

2 lines (multiline) picker for long text

why can i set the Row hight or multi lines for a picker,
the problem i have long texts for my picker, or gives an line break in picker... xcode 5 ...
i get the values for the picker from an array.
my code for my custom Picker is:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[UILabel alloc] init];
CGRect frame = CGRectMake(0,0,265,40);
label.backgroundColor = PickColor;
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18 ];
label.textAlignment = NSTextAlignmentCenter;
if(component == 0)
{
label.text = [_vergehen objectAtIndex:row];
}
return label;
}
thanks for help
Try this code
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 50;
}
then
- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 310, 50)];
lblTitle.numberOfLines=2;
lblTitle.textColor=[UIColor blackColor];
lblTitle.font=[UIFont systemFontOfSize:14];
lblTitle.text=[selectionList objectAtIndex:row];
return lblTitle;
}
Because you're creating a label, you might try this property of UILabel:
// UILabel wraps text across multiple lines
label.lineBreakMode = UILineBreakModeWordWrap;
If you set label.numberOfLines = 0, you should be able to get any number of lines placed within the label.
UILineBreakModeWordWrap is deprecated deprecated in iOS 6.0
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;

UIPickerView - multi-line rows - need layout advice

I want to make picker with 2 lines, I tried it like a link , but I can not understand what I need to do: create a view and 2 label on it, when I add labels coordinates to code, but selection field still like it as default. How can I change selection field size? And text in selection field have bigger size when other lines. Sorry for my english.
- (UIView*)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UIView* v;
if (view)
v = view;
else
{
v = [[UIView alloc] init] ;
UILabel* l1 = [[UILabel alloc] init];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] init];
l2.tag = 12;
l2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[v addSubview: l2];
}
UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 110, 35)];
l1.font = [UIFont systemFontOfSize:22]; // choose desired size
l1.text = [NSString stringWithFormat: #"row %d line 1", row];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 34 , 110, 35)];
l2.font = [UIFont systemFontOfSize:14]; // choose desired size
l2.text = [NSString stringWithFormat: #"row %d line 2", row];
l2.tag = 12;
[v addSubview: l2];
return v;
}
UPDATE
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return yourViewHeight;
}
- (UIView*)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIView* v;
if (view)
v = view;
else
{
v = [[UIView alloc] initWithFrame:CGRectMake(0, 34, 110, 35)] ;
UILabel* l1 = [[UILabel alloc] init];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] init];
l2.tag = 12;
l2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[v addSubview: l2];
}
UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 110, 35)];
l1.font = [UIFont systemFontOfSize:22]; // choose desired size
l1.text = [NSString stringWithFormat: #"row %d line 1", row];
l1.tag = 11;
[v addSubview: l1];
UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 34 , 110, 35)];
l2.font = [UIFont systemFontOfSize:14]; // choose desired size
l2.text = [NSString stringWithFormat: #"row %d line 2", row];
l2.tag = 12;
[v addSubview: l2];
return v;
}
http://i.picresize.com/images/2013/12/11/IiYqd.png
Hope It would work...
Implement this delegate method and return your view's height
(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return yourViewHeight;
}
Set frame for your label and your view..Frame of your label does not exceed the view's frame
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
//1. Create your view and set frame for the view
//2. Create your label 1 and label 2 set the frame for your labels
//3. Add and return your view
}
You should implement the pickerView:rowHeightForComponent: picker view delegate method and return a height tall enough for your custom view.
You should also set the height of v in your pickerView:viewForRow:forComponent:reusingView: method.
And lastly, if you are reusing a view, don't keep adding more labels. They will already be there from the reused view.
Try this code
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
than after
- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 310, 50)];
lblTitle.numberOfLines=2;
lblTitle.textColor=[UIColor blackColor];
lblTitle.font=[UIFont systemFontOfSize:14];
lblTitle.text=[selectionList objectAtIndex:row];
}
return lblTitle;
}

UISegmentedControl not responding

I'm adding a UISegmentedControl to the foot of a grouped UITableView programmatically. It's showing up fine, but when I tap on any of the options, they don't highlight. What am I doing wrong here? Also, how do I set a default item to be highlighted?
- (void)viewDidLoad
{
[super viewDidLoad];
// set up cacheControl
NSArray* cacheDays = [NSArray arrayWithObjects: #"1", #"2", #"3", #"4", #"5", nil];
self.cacheControl = [[UISegmentedControl alloc] initWithItems:cacheDays];
[self.cacheControl setTintColor:[UIColor blackColor]];
[self.cacheControl addTarget:self action:#selector(cacheSelection:) forControlEvents:UIControlEventValueChanged];
self.cacheControl.frame = CGRectMake(10, 16, self.tbl.frame.size.width-20, 47);
[self.cacheControl setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
}
- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{
switch (section) {
case 0:{
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbl.frame.size.width, tbl.contentInset.top)];
footerView.backgroundColor = [UIColor clearColor];
return footerView;
}
case 1:{
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbl.frame.size.width, 16)];
footerView.backgroundColor = [UIColor clearColor];
[footerView setClipsToBounds:NO];
[footerView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
[footerView addSubview:self.cacheControl];
return footerView;
}
}
return nil;
}
Screenshot, as requested:
To set a default item, do: [cacheControl setSelectedSegmentIndex:0] with the index that you want selected.
You should implement -tableView:heightForFooterInSection: from the UITableViewDelegate protocol.
This will let the table view adjust the height of the section footer to fit your view and allow it to receive user interactions.

Resources