Pin a UISegmentedControl underneath UINavigationBar in a UITableView - ios

How do I pin a UISegmentedControl underneath a UINavigationBar with a UITableView?
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"One", #"Two", nil]];
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
self.tableView.tableHeaderView = segmentedControl;
I've set up this UISegmentedControl like this so far, however, it scrolls with the rest of the UITableView content.

Try to use segmentedControl as your section header view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"One", #"Two", nil]];
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
return segmentedControl ;
}

Try the below code
NSArray *keyWordsList;
segmentControl = [[[UISegmentedControl alloc] initWithItems:keyWordsList] autorelease];
[segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segmentControl setFrame:CGRectMake(10, 47, 300, 30)];
[segmentControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[segmentControl addTarget:self action:#selector(segmentColorAction:) forControlEvents:UIControlEventValueChanged];
[segmentControl setSelectedSegmentIndex:0];
segmentControl.tag = 2;
//[self.navigationController.navigationBar addSubview:segmentControl];
[self.view addSubview:segmentControl];
segmentControl.hidden =YES;
[segmentControl setTag:kTagFirst];
kTagFirst provide value

Related

UISegmentedControl sticky mode

Is it possible to have a UISegmentedControl stick when it is selected, rather than just act as a normal button? In this sense they would be more like a UISwitch.
Here is my code:
segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"1",#"2",#"3",
nil]];
segmentedControl.frame = CGRectMake(0, 0, 120, 30);
[segmentedControl setWidth:40.0 forSegmentAtIndex:0];
[segmentedControl setWidth:40.0 forSegmentAtIndex:1];
[segmentedControl setWidth:40.0 forSegmentAtIndex:2];
[segmentedControl addTarget:self action:#selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.momentary = YES;
self.navigationItem.titleView = segmentedControl;
Here is a video of what is happening: https://www.dropbox.com/s/laijgt3zjdaya1z/Segmented.mov?dl=0
A UISegmentedControl "sticks" when it is selected by default. You're getting the behavior you describe because you set the momentary property to YES. The default is NO, so just delete that line.

How can I make button in section header for uitableview stay selected

I have a button that I want to change the background image if its selected. The code I have here works but if I scroll up and down again it becomes unselected.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
sectionHeaderLabel.text = [NSString stringWithFormat:#"Section %i",section+1];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
[button setBackgroundImage:[UIImage imageNamed:#"unselected_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeader addSubview:button];
return sectionHeader;
}
- (void)buttonPressed:(id)sender {
UIButton *allButton = (UIButton*)sender;
allButton.selected = !allButton.selected;
}
1) Create an image name array in you ViewDidLoad. This array tracks the name of you photos whether if its been clicked or not.
- (void)viewDidLoad
{
//this array is just an example, loop and add items to the array for the number of sections you want
//in this example there are 5 sections with 5 images
imageNameArray = [[NSMutableArray alloc] initWithObjects:#"unselected_image", #"unselected_image", #"unselected_image", #"unselected_image", #"unselected_image", nil];
}
2) Your viewForHeaderInSection
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
sectionHeaderLabel.text = [NSString stringWithFormat:#"Section %i",section+1];
button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
[button setBackgroundImage:[UIImage imageNamed:[imageNameArray objectAtIndex:section]] forState:UIControlStateNormal];
[button setTag:section];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeader addSubview:button];
return sectionHeader;
}
3) If button is pressed change the name of you image in the Array
- (void)buttonPressed:(id)sender
{
UIButton *allButton = (UIButton*)sender;
[imageNameArray insertObject:#"selected_image" atIndex:[allButton tag]];
[allButton setBackgroundImage:[UIImage imageNamed:[imageNameArray objectAtIndex:[allButton tag]]] forState:UIControlStateNormal];
}
What happens here is that every time the view becomes visible, you actually create a completely new view with everything reset. Instead of doing that you could simply save the view and re-use the same view every time. Create the view in viewDidLoad instead:
- (void)viewDidLoad
{
sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
sectionHeaderLabel.text = [NSString stringWithFormat:#"Section %i",section+1];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
[button setBackgroundImage:[UIImage imageNamed:#"unselected_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeader addSubview:button];
}
viewForHeaderInSection simply becomes:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return sectionHeader;
}
Please note that if you have more than one section you would need to create an array of section header views and return the correct one for each section, based on the value of the section in viewForHeaderInSection
That happen because when table scroll up and down it will again create cell so you have to manage it in some way which button is selected or not and again set selected state to those button which are previously selected in viewForHeaderInSection you can do that as bellow
//Define in header file
NSMutableArray *mutArrSelected;
//Initlise in viewdidload
mutArrSelected = [[NSMutableArray alloc] init];
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(mutArrSelected.count < section)
{
[mutArrSelected addObject:[NSNumber numberWithBool:NO]];
}
UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
sectionHeaderLabel.text = [NSString stringWithFormat:#"Section %i",section+1];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
if([[mutArrSelected objectAtIndex:section] boolValue])
{
button.selected = YES;
}
button.tag = section;
[button setBackgroundImage:[UIImage imageNamed:#"unselected_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeader addSubview:button];
return sectionHeader;
}
- (void)buttonPressed:(id)sender
{
UIButton *allButton = (UIButton*)sender;
allButton.selected = !allButton.selected;
[mutArrSelected removeObjectAtIndex:allButton.tag];
[mutArrSelected insertObject:[NSNumber numberWithBool:allButton.selected] atIndex:allButton.tag];
}

Programmatically add a UISegmentedControl to a UINavigationBar

How do I PROGRAMMATICALLY add a UISegmentedControl to a UINavigationBar?
I do not want to use a XIB file for this.
I have a UIView with a UITableView that is added as a subview.
I have tried two methods but both are not satisfactory:
1)
self.segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"All",#"Subject",#"Category",#"Finished",nil]];
self.segmentedControl.backgroundColor = [UIColor cloudsColor];
[self.segmentedControl setSelectedSegmentIndex:0];
[self.segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue) forControlEvents:UIControlEventValueChanged];
self.mainView.tableHeaderView = self.segmentedControl;
The reason this first one fails is that in the UITableView, when the user scrolls, the segmented control is scrolled as well! I don't want that to happen. It must stay pinned to the top.
2) Second attempt
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Filter_Personnal", #"Filter_Department", #"Filter_Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
self.navigationItem.titleView = statFilter;
This removes my title!!! I have a title at the top in the UINavigationBar and this method removes it!!
Here's an example of what I want to accomplish: UISegmentedControl below UINavigationbar in iOS 7
The UISegmentedControl must stay pinned below as part of the UINavigationBar and it must be below the title!
Thanks!
Use tableView:viewForHeaderInSection: (and possibly tableView:heightForHeaderInSection:) instead of tableHeaderView. Set tableStyle to UITableViewStylePlain (this should be the default).
You can use the following code :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 0) {
UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"All",#"Subject",#"Category",#"Finished",nil]];
segmentedControl.backgroundColor = [UIColor cloudsColor];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 20, viewHeader.frame.size.width, 50);
[viewHeader addSubview:segmentedControl];
return viewHeader;
}
return nil;
}
Hope this helps.
Thanks.
As the other posters have suggested, you could put the segmented control above the table view and under the nav bar, but you'd need to shift down the table view.
...Or, you could add the segmented control as a tableHeaderView.
A third option is to actually add it to the navigation bar. To do that you have to turn it into a navBarItem. Something like this:
UISegmentedControl *statFilter = [[UISegmentedControl alloc]
initWithItems:
[NSArray arrayWithObjects:
#"Filter_Personnal",
#"Filter_Department",
#"Filter_Company",
nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
UIBarItem *theBarItem = [[UIBarItem alloc] initWithCustomView: statFilter];
self.navigationItem.rightBarButtonItem = theBarItem;

How to change Bar Button Item colour?

In my detail view controller (part of navigation controller application) I've added custom "Back" button, like this:
- (void)loadView
{
[super loadView];
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 10.0f, 24.0f)];
UIImage *backImage = [UIImage imageNamed:#"left_arrow_icon"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
-(void) popBack {
[self.navigationController popViewControllerAnimated:YES];
}
As you can see I added left bar button and assign "popBack" action. Basically left_arrow_icon is silver but when I press it, iOS change it darker grey.
My question is, can I (and how) change initial colour to white? Is that possible?
Edit. I use xcode5
Edit2:
That does't work too
Try this code. It works perfectly for me -
UIImageView *navigationBarImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Navigation.png"]];
navigationBarImage.frame = CGRectMake(0, 0, 320, 44);
navigationBarImage.userInteractionEnabled = YES;
navigationBarImage.backgroundColor = [UIColor colorWithRed:28.0/255.0 green:53.0/255.0 blue:102.0/255.0 alpha:1.0];
[self.view navigationBarImage];
[navigationBarImage release];
UIButton *backBarBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
backBarBtn1.frame = CGRectMake(13, 12, 20, 20);
[backBarBtn1 setImage:[UIImage imageNamed:#"ic_back3.png"] forState:UIControlStateNormal];
backBarBtn1.backgroundColor = [UIColor clearColor];
[backBarBtn1 addTarget:self action:#selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBarBtn1];
Try this code -
[backbutton setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:HELVETICA_REGULAR_FONT size:17.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

Cant set UISegmentedControl to be clear

What am I doing wrong to set the segmentedControl's colour to clear, it is currently black?
UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Info", #"People", #"Photos", #"Items", nil]];
[control setFrame:CGRectMake(0, 45, self.frame.size.width, 40)];
[control setBackgroundColor:[UIColor clearColor]];
[control setSegmentedControlStyle:UISegmentedControlStyleBar];
[control setSelectedSegmentIndex:0];

Resources