Add UIImage and Segmented Control to Navigation Bar Center - ios

I am trying to add both an UIImage AND an UISegmentedControl to the middle of the navigation bar.
I can successfully set one of them to the middle of the nav bar with
[[self navigationItem] setTitleView:segmentedControl];
But only one of them can be displayed using setTitleView. Is there anyway to add the UIImage maybe as a subview IN ADDITION to the segmented control? For example, here is my not-working code to try to add two uielements onto the center of the tab bar.
UIImageView *logoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
logoImage.image = [UIImage imageNamed:#"segControlIcon.png"];
[[self navigationItem] setTitleView:logoImage];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[NSString stringWithString:NSLocalizedString(#"One", #"")],
[NSString stringWithString:NSLocalizedString(#"Two", #"")],
nil]];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl addTarget:self action:#selector(changeSearchType:)
forControlEvents:UIControlEventValueChanged];
[segmentedControl setFrame:CGRectMake(0,0,150,35)];
[[self navigationItem] setTitleView:segmentedControl];
Is this even possible? Thanks.

Related

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;

Trouble adding custom button to navigation controller's toolbar

I have a tableview controller that has a navigation controller. I have added a toolbar to the bottom of the view and now I need to add a custom button. I have the following code (from viewDidLoad), which is producing strange results. When the view loads, I see the custom button for a brief second, then an empty toolbar. At the moment I am only trying to get one custom button to load.
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height + 20;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - toolbarHeight,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage
imageNamed:#"stub_fav.png"]
style:UIBarButtonItemStylePlain
target:self action:#selector(addFavorite)];
[btn setTintColor:[UIColor whiteColor]];
[btn setImage:[UIImage imageNamed:#"stub_fav.png"]];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:btn, nil];
[self.navigationController.toolbar setItems:toolbarItems];
Any ideas? Thanks!
Im not sure what btnAddFave is for but it's not the subview of anything. I assume btw and btnAddFave should have the same background image, if so you spelled stub_fav.png wrong. shouldn't it be stub_fave.png

Create custom UI bar button (to change view from list view, grid view)

I am trying to create ui bar button like this to change view.
However, I don't know how to do to put 2 ui bar buttom together (1 square button and 4 square button). I only know how to do something like this. How should I do?
It is UISegmentedControl not UIButton
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] init];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"0.png"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"1.png"] atIndex:1 animated:NO];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[[[self navigationController] navigationItem] setRightBarButtonItem:barButtonItem];

bar button item image is turning to white icon even if its black

I am using these icons as bar button item in a toolbar
http://stopiransnukes.org/images/home_icon.png
and
http://www.bladmuziekplus.nl/shop/files/images/icon_home.png
But background image is converted to white automatically even if its black.
I tried setting image background via code but still its turning into white.
Manually : I have Dragged+Dropped the above image to the project.I have selected the bar button item and on 4th option on the right bar (shield icon) select different tint and under bar button drop-down image is set to the above image.
Programmatically :
[self.homeButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
[[self.homeButton] setImage:[UIImage imageNamed:#"home_icon.png"]];
How do I fix it ?
Thanks in Advance.
.
This is a step by step process to add a image into UIButton and add that UIButton as a custom view inside the UIBarButtonItem in the storyboard itself.. I will add some images for you to understand this totally..
step 1: Drag and drop the toolbar in storyboard view
step 2: Drag and drop the UIButton in your view
step 3: Drag and drop the image in your UIButton
step 4: Drag and drop the UIButton in the toolbar and now you can in the right side that it is added inside of the UIBarButtonItem. now you just have to connect an action method for your button.
viewDidLoad
[super viewDidLoad];
//uıbarButtonItem firstButtonItem,secondButtonItem
UIImage* firstButtonImage = [UIImage imageNamed:#"firstImageName"];;
CGRect frameimg = CGRectMake(0, 0, 30, 30);
UIButton * someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:firstButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(goToUserProfile)
forControlEvents:UIControlEventTouchUpInside];
self.firstButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
UIImage* secondButtonImage = [UIImage imageNamed:#"secondImageName"];
frameimg = CGRectMake(0, 0, 30, 30);
someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:secondButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(likePhoto)
forControlEvents:UIControlEventTouchUpInside];
[someButton setTag:1];
self.secondButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.toolbar setItems:[self addItemsToToolbar]];
AddItemsToToolbar Method
- (NSMutableArray *)addItemsToToolbar{
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[items addObject:flexSpace];
[items addObject:self.firstButtonItem];
[items addObject:flexSpace];
[items addObject:self.secondButtonItem];
return items;
}
This is the normal behaviour, you can read about it in the Human Interface Guidelines.
http://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html#//apple_ref/doc/uid/TP40006556-CH14-SW8
If you want to change that you need to write your own subclasses.
UIImage* infoButtonImage = [UIImage imageNamed:#"info_button.png"];;
CGRect frameimg = CGRectMake(0, 0, infoButtonImage.size.width, infoButtonImage.size.height);
UIButton * someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:infoButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(actionOfButton)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:someButton];
This code is going to solve your problem

Can set background image for UIBarButtonItem in nav bar, but not bottom toolbar

So this works when adding the item to the nav bar, but when i add it to the toolbar set in the bottom bar via interface builder, the background image doesn't show up.
UIBarButtonItem *resetButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Reset" style:UIBarButtonItemStylePlain target:self action:#selector(resetCriteria:)];
UIImage *img = [UIImage imageNamed:#"someImage.png"];
img = [img stretchableImageWithLeftCapWidth:5 topCapHeight:20];
[resetButtonItem setBackgroundImage:img forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.toolbarItems = [NSArray arrayWithObjects: resetButtonItem, nil];
Not only does the background not appear, none of the other behaviors work as well (but they work fine when adding these barbutton items to the nav bar)
You should try the following method to do the same.
Setting custom view in UIBarButtonItem.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 30, 30);
[btn setImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(resetCriteria:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *Item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.toolbarItems = [NSArray arrayWithObject:Item];
[Item release];
We can set any view in UIBarButtonItem with CustomView init method.
We could set the ImageView. But ImageView doesnot handle UIControl Events.

Resources