I have this code in my applicationDidFinishLaunching:
navController.toolbarHidden = NO;
[navController toolbar].tintColor = [UIColor darkGrayColor];
[[navController toolbar] setFrame:CGRectMake(0.0,0.0,320.0,180.0)];
The first two lines definitely have an effect on the UI. For example, if I set toolbarHidden to YES, it is certainly hidden. However, when I try to set the frame and customize the toolbar height, no change takes place. Does anyone know how to fix this?
UIToolbars have a fixed height. You won't be able to change it.
You have to make your own toolbar class if you need one with an adjustable height.
if you were to create your own toolbar, you can just use the frame property of the toolbar to create it. UINavigationController's toolbar height is not adjustable.
Related
I'm trying to set the height of a UITextView from a UIViewController's viewWillAppear event, here's the code:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
textView.text = blah blah...
textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y,
textView.frame.size.width, textView.frame.size.height-20);
}
However, it seems to ignore my sizing, it's as though after this event is called, the textview is resized by something else.
What am i doing wrong? Is there some good way to do this?
Thanks
-edit-
Got it working by putting the code in my viewDidAppear instead of the viewWillAppear.
So it works now.
Although i'm puzzled why i need to do this (remove 20 pixels from the bottom) - i mean shouldn't it resize to fill automatically? Why is it resizing to 20px too long? There's a tab bar at the bottom and a nav bar up the top. Is that confusing it?
Simple but obvious question, does the text change, in other words, is the outlet hooked up in IB?
JUSTA TIP: Look at using the helper functions for CGRect such as:
textView.frame = CGRectInset(textView.frame, 0, 20);
In an app with just this code and a single TextView the resizing occurs as expected. So..it's definitely something else in your app.
Are you maybe referencing the contentsize property or something else in your viewDidAppear?
Got it working by putting the code in my viewDidAppear instead of the viewWillAppear.
So it works now.
Although i'm puzzled why i need to do this (remove 20 pixels from the bottom) - i mean shouldn't it resize to fill automatically? Why is it resizing to 20px too long? There's a tab bar at the bottom and a nav bar up the top.
In my applicationDidFinishLaunching I set up a UINavigationController:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
navController = [[UINavigationController alloc] init];
[[navController navigationBar] setFrame:CGRectMake(0.0,0.0,320.0,20.0)];
...
}
As you can see, I am trying to make the navigation controller's height 20px. However, this is not working. I would imagine setFrame must be the correct function but I am not calling it in the right place. I realize that other questions on SO are similar to mine, but I think setting the navigationBar height should be possible if it responds to setFrame...right?
Also, anyone know the default height of the navigationBar?
Thanks!
The default height of the navigation bar is 44px.
I am not sure you should be attempting to directly resize the navigation bar, the documentation certainly discourages it:
When used in conjunction with a
navigation controller, there are only
a handful of direct customizations you
can make to the navigation bar.
Specifically, it is alright to modify
the barStyle, tintColor, and
translucent properties of this class,
but you must never directly change
UIView-level properties such as the
frame, bounds, alpha, or hidden
properties directly.
Depending on what you are trying to do it may be easier to place a new UIView over/below the navigation bar.
Default height is 44 pixels for navigationbar.
If you want to change it, try adding a category to UINavigationBar and override setFrame: method.
I have an application where a UIToolBar is to be constantly on the bottom side of the screen. No matter the device orientation.
The toolbar must have the same screen width and always be justified to the bottom side.
If the device rotates, the toolbar has to assume the new width and continue to be justified on the bottom.
Is there a way to do that programmatically?
thanks for any help.
First off, auto-rotation can be a little tricky. For example, if you are using a UITabBarController, you will need to subclass it and add the appropriate delegate methods. You should read up on auto-rotation via the Apple docs and Google, before really diving in.
To answer your question specifically though, here is how you would need to declare the UIToolbar such that it will auto-rotate when you have the app set up to do so:
// I keep this definition in a file called constants.h since I use it a lot
#define SCREEN_FRAME [[UIScreen mainScreen] applicationFrame]
UIToolbar *tb = [[[UIToolbar alloc]init]autorelease];
// this frame will position a toolbar at the bottom of the screen
tb.frame = CGRectMake(0,
SCREEN_FRAME.size.height-tb.frame.size.height,
SCREEN_FRAME.size.width,
tb.frame.size.height);
//Setting the auto-resizing mask will make the toolbar resize when the viewController
//it resides in rotates.
tb.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Yes. Have a look at autoresizing masks.
Use a navigation controller, and make use of the toolbarItems property.
I have a subclass of UIView that I've added to as the titleView of a navigationItem using the following line of code:
self.navigationItem.titleView = tempview;
Easy enough. That works fine. My problem is that this navigationItem sometimes has the rightBarButton updated (sometimes there is no button, sometimes there is one standard sized button, sometimes there is a larger button).
I figured that I could simply use the layoutSubviews method of the tempview class that I've added as the titleView so I put this in:
-(void)layoutSubviews {
[super layoutSubviews];
self.mylabel.frame = self.bounds;
}
This does not seem to work, as it does not correctly resize the titleview when the rightBarButton item is updated.
I've noticed also that the bounds do not grow once they gotten smaller, they simply change the position.
I've tried using setNeedsLayout and layoutIfNeeded but those simply "resize" the view with the incorrect bounds.
I've also made sure that the rightBarButton item is set to nil, but the view still does not correctly expand once shrunk.
Thanks for any help!
configure your view with
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
(probably in the initWithFrame)
then implement
- (void)willMoveToSuperview:(UIView *)newSuperview
{
[self setFrame:[newSuperview bounds]];
}
now you have your view matching the size of the container, and resizing automatically.
By default, layoutSubviews does nothing. You also never change the size of the titleView, so unless a navbar does that for you, it's no surprise that nothing is changing.
Since you're only changing the size of one of the subviews, I don't think autoresize will work, as I'm pretty sure it's triggered when the superview (the one with autoresizesubviews enabled) changes size. I would suggest recalculating the size of the titleview when you change the rightbutton. If it automatically fits when you add it the first time, you could remove and readd it, but I know that's a pretty ugly hack.
Instead of overriding -layoutSubviews have you tried setting the autoresizingMask property?
tempView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
I am using a navigation controller, and I have the style set to :
navController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
But when I run my program, the navigation controller looks like it is on top of a white background, not my background. When I push a controller, left or right, all my view, the current one, shifts to the top exactly the size of the navigation bar. And it is there where I can see my background through the navigation controller bar. Any ideas? When my barStyle is set to opaque, everything looks fine. I was thinking on setting my view frame a negative 'y' value, but I think there should a more elegant way.
I believe the UINavigationController assumes that your controller view frames don't include the area beneath the navigation bar.
UIBarStyleBlackTranslucent is more often used for UIToolbar, so Apple probably didn't make it easy to use it nicely with UINavigationBar. You'll probably need to abandon the UINavigationController, or start hacking the frames (careful with rotations), if you want to reliably render under the bar area.
Also, if your intention is to hide the navigation bar after a few seconds, you'll have a much easier time if you make it fade out (like the Photos app) instead of trying to slide it up (like Mobile Safari). Trust me on that one... that took me a lot of time to learn the hard way.
Simply use a transparent background image, and translucent = YES to allow the content to flow below the bar. Works on iOS 5 / 6. Add in viewDidLoad.
self.navigationController.navigationBar.translucent = YES;
UIImage * backgroundImage = [UIImage imageNamed:#"spacer.gif"];
[self.navigationController.navigationBar setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:UIBarMetricsDefault];
I attached the spacer.gif image here, a single 1px x 1px transparent image.
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.169 green:0.373 blue:0.192 alpha:0.9];
self.navigationController.navigationBar.translucent = YES;
Note:
Don't use self.navigationBarStyle and self.navigationBarTintColor to change.
Add the last two statements to your viewDidLoad.
I ran into this same problem (in 3.1.3) and while you can't set the bar style after the navigationBar has already been setup you CAN set the tintColor and translucent values whenever you like:
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.navigationController.navigationBar.translucent = YES;
Will create the 'blackTranslucent' bar, I change the navigationBar look when I push certain view controllers onto the stack.
I had the same problem, and I solved it by making the background of the root view the same as my view. The white area behind the navigation bar turned out to be the root view.
The navigation controller offsets the coordinate sytem of all it's subviews so they draw below the navigation bar.
Extend your view's frame into the negative y domain for it to draw under the navigation bar.
You need to set the barstyle in your info.plist file for it offset everything correctly.
However, I haven't tried it since the 2.1 f/w was released, but when I tried this in 2.0 I found that the setting was lost after a rotation from portrait to landscape.
try to use this, may be it will helpful.
_topToolBar.barStyle = UIBarStyleBlackTranslucent;
_topToolBar.alpha = 0.3;
I had a same problem.I solved!
ImageViewExtendController *detailImageController = [[ImageViewExtendController alloc] init];
[detailImageController loadImage:url];
[self.navigationController pushViewController:detailImageController animated:YES];
If you set your nav controller's navigationBar to transparent in your App delegate early enough (It worked for me before adding the nav controller to the window), it will automatically shift your view up underneath the navigation bar.
Unfortunately it does not also shift your view underneath the status bar. Sad, it looks like you need to implement your own version of UINavigationController. Luckily, it's not too bad as UINavigationBar is pretty reusable.
Try this:
self.tabBarController.tabBar.superview.backgroundColor = [UIColor blackColor];
Change the Extend Edges options in child viewControllers
As for example, in xcode editor, go to your first viewcontroller child and unset the options:
Extend Edges;
Under Top Bars;
Under Bottom Bars;
Under Opaque Bars;
This way your child ViewController will not layout starting below the status bar of the navigation controller, neither the tabbar or the toolbars
hope it may help anyone