How do i hide the Separator in my Picker View.Here is the screenshot .
Here is the code for my custom UIPickerView.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label=[[UILabel alloc]init];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.textAlignment=NSTextAlignmentCenter;
switch (component) {
case 0:
label.text=[_hourArray objectAtIndex:row];
label.font = [UIFont fontWithName:#"MYRIADPRO-REGULAR" size:70];
break;
case 1:
label.text=[_minutesArray objectAtIndex:row];
label.font = [UIFont fontWithName:#"MYRIADPRO-REGULAR" size:70];
break;
case 2:
label.text=[_ampmArray objectAtIndex:row];
label.font = [UIFont fontWithName:#"MYRIADPRO-REGULAR" size:15];
break;
default:
break;
}
return label;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
Please Help me out.Thanks
To hide the selection indicator for a UIPickerView:
_pickerView.showsSelectionIndicator = FALSE;
You can make it in code (as above) or in Interface Builder:
Edit
According to Apple documentation:
On iOS 7 and later you cannot customzie the picker view’s selection
indicator. The selection indicator is always shown, so setting this
property to NO has no effect.
In Swift 5 you can use this workaround. Just add this code to your viewcontroller class:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for i in 1...2 {
myPickerView.subviews[i].isHidden = true
}
}
If you really need, here is a pragmatic solution: Create a subclass of UIPickerView where you override didAddSubview. If the added subview has a height <= 1.0, then it's a separator and you can hide and/or remove it.
NOTE: Might break in future iOS versions, so handle with care.
Related
So I have this Tableview with a few sections, (3) to be exact. I want there to be headers for sections 2 and 3, not for the first sections..
Heres what i've done:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionName;
UIView *tempView;
tempView = [[UIView alloc]initWithFrame:CGRectMake(0,0,300,20)];
tempView.backgroundColor=[UIColor grayColor];
UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,0,300,20)];
tempLabel.backgroundColor = [UIColor clearColor];
tempLabel.shadowColor = [UIColor blackColor];
tempLabel.shadowOffset = CGSizeMake(0,2);
tempLabel.textColor = [UIColor whiteColor]; //here you can change the text color of header.
tempLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0f];
switch (section)
{
break;
case 1:
{
sectionName = NSLocalizedString(#"Information", #"Information");
tempLabel.text = sectionName;
[tempView addSubview:tempLabel];
}
break;
case 2:
{
sectionName = NSLocalizedString(#"Tools", #"Tools");
tempLabel.text = sectionName;
[tempView addSubview:tempLabel];
}
break;
}
return tempView;
}
I am confused on what needs to be done... Heres a pic of whats going on :
In the case of section == 0, you are still setting tempView to a new instance of UIView and returning that value, you just aren't setting the title of the label. Also as you learned you should return 0 for tableView:heightForHeaderInSection: for section 0.
So while I was writing this question I cam to a conclusion... but maybe its not the more efficient?
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat headerheight = 20.0;
switch (section)
{
case 0: { headerheight = 0.0; } break;
case 1: { headerheight = 20.0; } break;
case 2: { headerheight = 20.0; } break;
}
return headerheight;
}
If anyone has any suggestions on how I don't need to implement this tableview delegate method, please speak up. I feel like I just wouldnt need to return a view for the specified section rather than say a sections header is 0. But I'm not completely sure at the moment, the problem is SOLVED but maybe not solved correctly?
Heres a pic of the result of the solution.
i currently have a uipicker with two components. The first contains images and works fine, the second however requires integer values.
Is this possible since the following callback method returns a UIView type not integer? Surely there must be a way around this?
-(UIView *)pickerView
any help would greatly be appreciated, many thanks
this?
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
switch (component) {
case 1:
{
UILabel *intvalue = view?(UILabel *)view:[[[UILabel alloc] initWithFrame:CGRectMake(0,0, 50, 40)] autorelease];
intvalue.text = [NSString stringWithFormat:#"%d",value];
intvalue.textAlignment = UITextAlignmentCenter;
intvalue.backgroundColor = [UIColor clearColor];
return intvalue;
break;
}'
}
i was just wondering if there was a way to change the fontSize of a row in a uipickerview
for a specific component and not for all of them. Also the different components still have to be able to display different things and not the same.
Does anyone have a solution for this problem? Thanks in advance!
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lbl = (UILabel *)view;
// Reuse the label if possible...
if ((lbl == nil) || ([lbl class] != [UILabel class])) {
CGRect frame = CGRectMake(0.0, 0.0, 270, 32.0);
lbl = [[[UILabel alloc] initWithFrame:frame] autorelease];
.... do what you like ...
}
lbl.textColor = [UIColor blackColor];
lbl.text = [self.<yourdata> objectAtIndex:row];
return lbl;
}
The calling parameters tell you which view, row and component you are setting. Check them and set the appropriate thing only if both the row and component match (compare and use flow control) what you want for that row and component.
I'm using a pickerView with multiple components related to several fields in a Database (CoreData).
Is it possible to change the fontcolor for a specific component according the presence of data in the DB ?
For example the field in the DB is null the component font color should be RED otherwise black.
Any help will be appreciated !
Dario
==================
Thanks Kenny,
I have to apply to a single UIPicker only. So I', returning the view parametere (without modificatiosn). The result is all the pickers show empty rows.
Thanks for help !
Here you will find the code fragment:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (pickerView == tipoPk){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100,30)];
label.textColor = [UIColor redColor];
switch (component) {
case PK_Tipo:
label.text = [tipoArray objectAtIndex:row]];
break;
case PK_Settore:
label.text = [settoreArray objectAtIndex:row]];
break;
default:
break;
}
return label;
}
else {
return view; // <==== return view for non related pickerviews , but no rows shown
}
}
This one also works..
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];
if (row == 0)
{
label.backgroundColor = [UIColor redColor];
}
if (row == 1)
{
label.backgroundColor = [UIColor blueColor];
}
if (row == 2)
{
label.backgroundColor = [UIColor blackColor];
}
return label;
}
Yes, but you have to use -pickerView:viewForRow:forComponent:reusingView: and supply a custom UILabel.
For an iPhone, I've got a UITableView that is grouped, has one section, and in which I've set up a section header that's a UILabel object from the nib. When the table view displays, the header shows up as a stripe of solid black -- no text.
In heightForHeaderInSection I've set the height to be the frame.size.height of the UILabel object. When I change the height in IB, the black stripe's height changes. So I know that the .m file has latched on to the right UILabel object.
In the debugger, in viewForHeaderInSection, it seems that the width of the UILabel object is zero, and the height is 1079574528, and the text is null.
Any thoughts on what I'm doing wrong?
Not sure what you're doing wrong, but here is some example code that might help (from a post on my blog):
#define SectionHeaderHeight 40
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([self tableView:tableView titleForHeaderInSection:section] != nil) {
return SectionHeaderHeight;
}
else {
// If no section header title, no section header needed
return 0;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(20, 6, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green
saturation:1.0
brightness:0.60
alpha:1.0];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];
return view;
}
I had the same issue and haven't quite figured why the black bar..
BUT, instead of providing the header and footer views in delegate methods,
if i set values for tableView.tableHeaderView and tableView.tableFooterView, its all fine !
Can you post the code for your heightForHeaderInSection and your viewForHeaderInSection functions? The theory behind what you're doing sounds correct, but without seeing the code, it would be nearly impossible to figure out the issue...
It sounds like you place a label on the view in IB and are trying to use that as your header view - which is not the proper way of doing things. If you aren't using viewForHeaderInSection, then give that a try.. like this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *lbl;
lbl.text = #"Header for The Only Section";
//define other properties for the label - font, shadow, highlight, etc...
return lbl;
}
3.1.3 doesnt like [UIColor clearColor]; try using the same background color as your tableview
I observed same behavior when refreshing after datasource is loaded.
I noticed this was due to the way i was refreshing table view.
//[self loadView]; this caused the section header to go black.
[self.tableView reloadData]; // this works!