I'm trying to add a Segmented Control to my UIToolbar below, but when I try to drag it over in Storyboard it replaces my Table View.
Also, when I try to add UIBarButton to my Toolbar it pushes my Prototype Cells down... do I have my views hierarchy wrong?
When I add a UIBarButton item anyway, in the simulator the toolbar is not Black Transparent as I set in my navigation control inspector. Whats up with that?
thanks!
To create Segmented Control element in the toolbar with Interface builder it is possible to use the following hint
1) Create Segmented Control Bar Button Item in Navigation Controller:
2) Drag Bar Button Item up to Navigation Item
3) Then you should get the following structure
4) Now you can select Segmented Control to set its properties
1 - Do it programmatically. You need to assign a frame to your UIToolbar first, then you can add elements in it (the segmentedControl). I suggest though to use UIBarButtonItems, which basically are the same.
To stick it to the bottom, try to do this way:
#define SCREEN_FRAME [[UIScreen mainScreen] applicationFrame]
CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 44);
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:frame];
toolBar.frame = CGRectMake(0,self.view.frame.size.height-toolBar.frame.size.height,SCREEN_FRAME.size.width,toolBar.frame.size.height);
//Setting up the items
UIBarButtonItem *first = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image.png"] style:UIBarButtonItemStylePlain target:self action:#selector(yourAction:)] autorelease];
UIBarButtonItem *second = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image.png"] style:UIBarButtonItemStylePlain target:self action:#selector(yourAction:)] autorelease];
//Creating an array with the items
NSArray *items = [NSArray arrayWithObjects:first,second, nil];
//Assigning the array to the toolBar
[toolBar setItems: items];
Then you can set its style with
mytoolbar.barStyle = UIBarStyleBlack;
2 - Same as above, framing.
3 - You need to set your navigationController style to Black.
Use self.navigationController.navigationBar.style = UIBarStyleBlack; ,or in IB choose the buttons and from Inspector set Tint or Style to whatever you want.
Check also this question
I presume there's a navigation view controller somewhere in here. They have their own toolbar, but it's hidden and empty. The toolbar you're seeing is just a simulation of a toolbar for the purpose of letting you edit the UI.
So to do this with storyboard, don't add the segmented control to the simulated toolbar, create a new toolbar and add it to the tree of views available in your controller (as a sibling to your Table View) and create an IBOutlet to reference it. Then in your viewDidLoad method assign the items in the toolbar to the toolbar already created by the view controller.
In this example I've created a property on my view controller called (cunningly) toolbar:
#property (nonatomic, retain) IBOutlet UIToolbar toolbar;
In my view controller's implementation I assign the toolbar items manually:
-(void)viewDidAppear:(BOOL)animated
{
[self setToolbarItems:self.toolbar.items animated:NO];
[self.navigationController setToolbarHidden:NO animated:YES];
}
You'll need to hide the toolbar when the view controller is popped, for instance in the calling view controller:
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setToolbarHidden:YES animated:YES];
}
Assuming you want to hide it, of course.
I did this by placing the UISegmentedController in a Bar Button Item.
Add a toolbar to the UINavigationController window, then enable the display of this toolbar on a ViewController screen.
Drag a bar button into the toolbar on the ViewController screen, and drag a UISegmentedController into that (resizing the button to the full width of the screen)
To wire it all up, ctrl-drag from the UISegmentedController to the corresponding .h file, drag from "referencing outlet" to the yellow view controller icon (using the same variable name), then drag from "ValueChanged" to the yellow icon (selecting the method on the controller that you want to call on a new selection).
This all seems to work as expected. (If you can't select a segment on the controller to set an initial value, you didn't connect up the referencing outlet as described above. That stumped me for a little while.)
Related
I want to add text field and search icon on navigation bar of tab-bar controller. Please check attach image.
Note: This is home screen and if user click on any menu item it will goes to detailvc so i add one navigation controller on homevc and hide it.
I want to show search icon and text-field at navigation-bar of tabbar. how can i do this ?
set textfield with left view in navigation title view
navigationItem.titleView = UITextField()
Subclass the navigationbar
#interface SearchNavigationBar : UINavigationBar
-(void)layoutSubviews
{
[super layoutSubviews];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"searchImage"] style:UIBarButtonItemStylePlain target:self action:#selector(searchPrompt:)];
self.topItem.rightBarButtonItem = buttonItem;
}
Add it as the class of your navigation controller
and get the object this way
UINavigationController *navBar = [[UINavigationController alloc] initWithNavigationBarClass:[SwitchAssessmentNavigationBar class] toolbarClass:nil];
and then you can call custom methods on the tool bar such as delegates that trigger events in your main page to change
searchBar in center of navigationBar:
self.navigationItem.titleView = self.searchBarTop;
searchBar in left/right side of navigationBar:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
Create an UIView custom class which will hold your custom search bar.
Then add that view's instance into your navigationController's view.
[self.navigationController.view addSubview:yourCustomView];
I have to add on own custom custom class of UItabbar-Contrller
AS you can see in the image below, Twitter use different navigation bar color for each view-controller that is pushed.
I've tried almost everything (setbackgroundimage, backgroundcolor, bartintcolor, etc) but nothing seems to work. What i think is that Twitter use custom transition to SIMULATE the push, because, what it seems to me is that every view-controller is presented has his own navigation bar with his own color.
If you want to handle navigationBar with different barTintColors, Code School had a tutorial about it. (iOS App: Creating a Custom Nav Bar)
It could also extended to different backgrounds by using setBackgroundImage:forBarMetrics: method.
There are following four steps:
Handle this in viewWillAppear of the source view controller, hide the navigationBar that navigationController provided and create a new navigationBar to the superView.
- (void)styleNavBar
{
// 1. hide the existing nav bar
[self.navigationController setNavigationBarHidden:YES animated:NO];
// 2. create a new nav bar and style it
UINavigationBar *newNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64.0)];
[newNavBar setTintColor:[UIColor whiteColor]];
// 3. add a new navigation item w/title to the new nav bar
UINavigationItem *newItem = [[UINavigationItem alloc] init];
newItem.title = #"Source";
[newNavBar setItems:#[newItem]];
// 4. add the nav bar to the main view
[self.view addSubview:newNavBar];
}
Do the same trick in viewWillAppear of the destination view controller, and create a backBarButtonItem as new navigationBar'sleftBarButtonItem.
- (void)styleNavBar
{
[self.navigationController setNavigationBarHidden:YES animated:NO];
UINavigationBar *newNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64.0)];
[newNavBar setTintColor:[UIColor blueColor]];
UINavigationItem *newItem = [[UINavigationItem alloc] init];
newItem.title = #"Destination";
// BackButtonBlack is an image we created and added to the app’s asset catalog
UIImage *backButtonImage = [UIImage imageNamed:#"BackButtonBlack"];
// any buttons in a navigation bar are UIBarButtonItems, not just regular UIButtons. backTapped: is the method we’ll call when this button is tapped
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(backTapped:)];
// the bar button item is actually set on the navigation item, not the navigation bar itself.
newItem.leftBarButtonItem = backBarButtonItem;
[newNavBar setItems:#[newItem]];
[self.view addSubview:newNavBar];
}
Fill out the backTapped: method so that user is able to tap-to-popover from destination view controller.
- (void)backTapped:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
Considering the swipe-to-pop situation, setting the gesture recognizer’s delegate to self in viewWillAppear of the destination view controller. (The author: The solution here is a bit of a hack.)
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self styleNavBar];
__weak id weakSelf = self;
self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;
}
This for swift3 :
You can set the original navbar background with an empty image:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
With this all of the others VC in the view hierarchy will adopt an transparent nav bar.
In the other VC if you want to set an custom image or a custom color simply put a background view in the position of the navbar, this in the view did load method of the particular view controller.
let viewNavBar = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 64))
viewNavBar.backgroundColor = UIColor.white
view.addSubview(viewNavBar)
Whit this you can set any background or image inside the viewNavBar and don't mess with the overall configuration of the navigation bar.
Twitter doesn't change the navigation bar colour. When you're looking at a user's profile, it's a blurred version of the user's cover photo.
As you can see in the transition, the whole user profile view replaces the previous view. The navigation bar doesn't change, it is replaced. They might not even use a UINavigationBar (or at least not the one from the navigation controller).
The "bar" is a custom view that shows the user's cover photo, with the back/search/tweet buttons appearing in their usual positions. The user's cover photo shrinks, blurs and attaches to the top of the screen when you have scrolled down - and at this point, it looks like a normal navigation bar. The user's name and tweet count also scrolls up to the center of the navigation bar at this point.
It's quite intriguing, and their whole view structure for a user's profile probably uses a bunch of tricks. But it's not exactly a simple task to imitate their profile view, and they do much more than just change the tint of their navigation bar. If you just want to do this, Undo's answer works well. However, you may also have to reset the tint colour in your viewWillAppear method (of the old and new views).
Try look up this GitHub repo, I think it could help you achieve that effect https://github.com/kingiol/KDInteractiveNavigationController
I have navigation controller based app and one viewcontroller presents modally graph in a landscape mode. I then add Toolbar with Done button to dismiss the graph vc and return to navigation and portrait mode.
I can't figure out how to position the Toolbar on top of the graph viewcontroller with correct shadow on the bottom of the toolbar. So far I have this code to add the toolbar to the bottom position, which has default shadow on the top of the toolbar. Is it allowed to have toolbar on top of the screen? For the reason of forced orientation rotation I cannot use navigation controller with the graph vc. Platform is iOS7 and iPhone only. Thanks.
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.width - 44.0, self.view.bounds.size.height, 44.0)];
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
toolbar.items = [NSArray arrayWithObjects:flexibleSpaceButtonItem, doneButtonItem, nil];
[self.view addSubview:toolbar];
I think your frame is looks a bit strange. You are calculating the y position from the view width and the width from the view height.
Maybe you have to specify that the toolbar is on top using the UIBarPositioning protocol.
UIImage *shadow = [toolbar shadowImageForToolbarPosition: UIBarPositionAny];
[toolbar setShadowImage:shadow forToolbarPosition:UIBarPositionTopAttached];
Next Edit:
This is what the documentation has to say about the iOS 7 UIToolbar:
UIBarPositionTop
Specifies that the bar is at the top of its containing view.
The system uses this as a hint to draw directional decoration accordingly. For example, any shadow would be drawn below the bar.
Instances of UIToolbar do not appear with this position on iPhone, but they can on iPad.
Available in iOS 7.0 and later.
Declared in UIBarCommon.h.
Maybe toolbars are not meant to be used on top. However, you can simply add a shadow with addSubview:
Try to implement the method
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
from of UIToolbarDelegate protocol.
My question is next:
In interface builder i create UINavigationBar and I want to create 'Back' button item, but I dont see any button.
I use this code:
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = #"Back";
mynavBar.backItem.backBarButtonItem = myBarButtonItem;
mynavBar - this is my IBOutlet.
Thanks for help!
You can use a navigation bar as a standalone control or in conjunction with a navigation controller. When you use a navigation bar as a standalone control you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed.
So in your case you would use something like this:
UIBarButtonItem *myBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
myBarButtonItem.title = #"Back";
UINavigationItem *right = [[[UINavigationItem alloc] initWithTitle:#"Hello!"] autorelease];
right.leftBarButtonItem = myBarButtonItem;
[mynavBar pushNavigationItem:right animated:YES];
You may want to look into using UINavigationViewController though.
If you want a custom button on the left, use mynavBar.leftBarButtonItem instead of backItem.
The backItem will only be visible, after you presented another viewcontroller via pushViewController:. (If you didn't set you own backbutton, the default backButton with the title of the previous viewController will be created automatically.)
//edit: perhaps you look for that:
Draw custom Back button on iPhone Navigation Bar
I think this is the actual way apple want this to be implemented.
Put UINavigationBar
Set outlet to the UINavigationItem
This is the catch
Override navigationItem property to return the UINavigationItem you created.
That's it.
-(UINavigationItem *) navigationItem
{
return self.navigationItem1;
}
If your navigationItem is still in the UINavigationBar, I think you will need to have a strong outlet to the UINavigation Bar too. Please correct me if I am wrong here.
This may not be possible, but I'm hoping someone will have an idea how to do it.
I have an app I'm porting from iPhone only to Universal. On the iPhone, I'm using a Tabbed application. I use three tabs for the normal data to be displayed. I have a forth tab that's only displayed if certain conditions are met. To add the tab, I do:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
UITabBarController *tabController = (UITabBarController *) self.rootViewController;
NSMutableArray* newArray = [NSMutableArray arrayWithArray: tabController.viewControllers];
[newArray addObject: [theStoryboard instantiateViewControllerWithIdentifier: #"AdditionalView-Phone"]];
[tabController setViewControllers:newArray animated:YES];
}
For the iPad, I have enough space on the initial view to display everything from the main three tabs in the iPhone UI. So all I need is one additional (small) view for the "Additional" data. I wanted to do it using a popOver view, so I set up the initial view with a Nav bar and popover button as in the Utility App template. But now I'm stuck. I can't figure out how to create that popover button at run time and make it do the segue to the popOver view properly. I can add the button like this:
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle: #"Modem" style: UIBarButtonItemStylePlain target: self action: #selector(togglePopover:)];
self.navBar.topItem.rightBarButtonItem = flipButton;
but I get an exception: 'NSInternalInconsistencyException', reason: 'UIStoryboardPopoverSegue must be presented from a bar button item or a view.' I'm pretty sure this is because I don't have an anchor set for the popOver segue. The button doesn't exist in the storyboard, so I can't set it there. And I can't seem to find an API to set it at run time.
I also tried creating the button in IB, but not in the view hierarchy, and then just setting the rightBarButtonItem property to my existing button. That also works, but I still can't set that button as the anchor for the popover view. I can set the Navigation Bar as the anchor, but that makes it anchor to the title in the nav bar, which looks silly.
Any ideas?
I had the same problem and solved it by creating a UIBarButtonItem in the Storyboard for the view controller but not part of the view hierarchy.
In IB, Drag a bar button item to the dark bar below the view controller view, drop it next to the "First Responder" and "View Controller" icons. Create a (strong) IBOutlet for it. Then create a popover segue from it to the destination view controller by dragging from the bar button item to the destination. It seems like this is the only way to set it as the anchor. Choosing it as the anchor for an existing segue does not work (looks like an IB bug).
In viewDidLoad you can assign this bar button item to the navigationItem (or where ever you like) and the segue works as expected.
I was curious about this too so I made a quick test project. You're right, there doesn't seem to be a way to configure the popover segue at runtime or add an anchor point to a button that's not in the view hierarchy using Interface Builder.
My solution was to set everything up in IB with the UIBarButtonItem visible and connected to an IBOutlet property, then remove it from the navigation bar in -viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = nil;
}
Then I simply add it back or remove it by tapping another button:
- (IBAction)toggleBarButtonItem:(id)sender
{
UIBarButtonItem *item = (self.navigationItem.rightBarButtonItem == nil) ? self.popoverBarButtonItem : nil;
[self.navigationItem setRightBarButtonItem:item animated:YES];
}
You could conditionally keep or remove the button in -viewDidLoad the same way. The segue remains anchored to the UIBarButtonItem.
I'm gonna try making a dummy view that's the size and shape of the views I want to present the popover from, wire that to the segue popover target, and then move the view to the right position in prepareForSegue:sender:
I'm not sure this is exactly what you want, but this is what I would do. Create a button and set it up with some target/action. Call that target/action method
presentPopover:(UIButton *)sender;
Then in the presentPopover method, say
UIViewController *customAdditionalViewController = [[MySpecialVC alloc] init];
//Configure the customAdditionalViewController
//Present the customAdditionalViewController in a Popover
UIPopoverController *popover = [[UIPopoverController alloc] initWithViewController:customAdditionalViewController];
//Set popover configurations
[popover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:/*whatever you want*/ animated:YES];
That is how I would handle your use case.