I want my UINavigationBar to rotate properly within my SignatureViewController. This is currently how it looks in Portrait vs Landscapemode when rotating: http://tinypic.com/view.php?pic=2w5573a&s=5#.Uk5kgYapWrg
As you can see the UINavigationBar does not scale on the width when in landscape. My project is built as following:
rootViewController(UINavgationController) ------Modal push-----> SignatureViewController
(portrait only) (portrait/landscape)
Note that I am not using a normal push to the SignatureViewController, I am using a modal one. Just in case I wanna mention this because I am not sure if it can affect the outcome in any strange way?... I have tried different approaches to change the background of the NavigationBar depending on if it is in landscape or portrait. I will post some here below:
try nr1: Not working
- (void) viewWillAppear:(BOOL)animated {
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"navbar25g.png"] forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
try nr2: Not working
- (void) viewWillAppear:(BOOL)animated {
UIImage *navBarLandscape = [[UIImage imageNamed:#"navbar25g.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(x,x,x,x)];
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance]setBackgroundImage:navBarLandscape forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
try nr3: Not working
- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
UIView *v = [[UIView alloc]init];
int a = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
int w = [[UIScreen mainScreen] bounds].size.width;
int h = [[UIScreen mainScreen] bounds].size.height;
switch(a){
case 4:
v.frame = CGRectMake(0,20,w,h);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(#"willrotate:1");
break;
case 3:
v.frame = CGRectMake(-20,0,w-20,h+20);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(#"willrotate:2");
[self.view addSubview:v];
break;
case 2:
v.frame = CGRectMake(0,-20,w,h);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(#"willrotate:3");
break;
case 1:
v.frame = CGRectMake(20,0,w-20,h+20);
NSLog(#"willrotate:4");
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
}
Note that in try nr3 the NSLog works but it refuses to change the background of the UINavigationBar. So, any ideas what I shall do from here ? / Regards
can you please try to implement the UINavigationBar class and override one drawRect: method
it should work
#implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect
{
UIImage *navimage;
//we will check if screen width is 320 then it will be potrait
if(self.frame.size.width == 320)
{
navimage = [UIImage imageNamed: #"Your Potrait nav bar image"];
}
else
{
navimage = [UIImage imageNamed: #"Your Landscape nav bar image"];
}
[navimage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Hope it helps...
and just try this for nr1 and nr2
just change the last line of code with this..if its working or not...let me know
self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; //UIViewAutoresizingFlexibleWidth
Related
At my main view, that contains a menu, I have an image on the navigationbar. When I select one of the menu items, I want to remove the background image and set text, normally.
When the second view opens, the background image still there. I tried everything, following some answers on StackOverflow, but still getting the same error.
I tried this:
- (void)viewWillDisappear:(BOOL)animated
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
[super viewWillDisappear:animated];
}
Didn't work. Tried this:
#implementation ViewController{
UIImage *_bgNavigationBarImage;
}
- (void) viewWillAppear:(BOOL)animated {
_bgNavigationBarImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigation-bar-home.png"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController.navigationBar setBackgroundImage:_bgNavigationBarImage forBarMetrics:UIBarMetricsDefault];
[super viewWillDisappear:animated];
}
Didn't work too. Tried to set nil to background and still doesn't working.
Done!
Used the first method, but changing this:
[[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
for this:
[self.navigationController.navigationBar setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
I have a custom navigation bar showing a logo with an image and the status bar appears white. I'd like to be able to set the status bar color to match the greyish background of the logo.
http://i.imgur.com/wn0oOX5.png
When a cell is selected, I have the navigation bar going back to default and the status bar matches the navigation bar.
http://i.imgur.com/btQOvXn.png
LogoNavBar.m:
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(self.frame.size.width, 90);
return newSize;
}
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"logo.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
MasterViewController.m:
- (void)viewDidLoad
{
[self.navigationController setValue:[[LogoNavBar alloc]init] forKeyPath:#"navigationBar"];
}
DetailViewController.m:
- (void)viewDidLoad
{
[self.navigationController setValue:[[UINavigationBar alloc]init] forKeyPath:#"navigationBar"];
}
I've tried:
[[UINavigationBar appearance] setBarTintColor:[UIColor *colorhere*]];
in the app delegate and elsewhere but to no luck.
I have those 2 issues in iOS7 that I can't figure out.
The first issue is that the TableView rows goes beneath the Status Bar, how can I disable it, or to make the section header of the table view to be under the status bar all the time?
The second issue that I have is on the navigation controller the status bar seems to be black with black background and I don't know how to fix it, the view controllers background is white, but the status bar reminds black and I don't know why.
UPDATE:
Still no answer.
I've got the answer in another post, please check it everyone who has problems with iOS6-7 and StatusBar:
UIStatusBar as in Facebook new iOS7 application
Enjoy!
I think you need to do following code.
You will have to check condition for ios7.
You will need these two delegate methods for tableview.
Check my code below.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
int versionValue=[currSysVer intValue];
if(versionValue>=7)
{
return 60;
}
return 0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 60)];
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
Hope this helps you.
use these lines of code
if ([self respondsToSelector:#selector(setNeedsStatusBarAppearanceUpdate)]) {
// iOS 7
[self prefersStatusBarHidden];
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
} else {
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
and
- (BOOL)prefersStatusBarHidden
{
return YES;
}
Answer for your second issue!
Put in your didFinishLaunchingWithOptions:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[self customizeios7];
} else {
[self customizeios6];
}
And make these 2 voids:
-(void)customizeios6 {
UIImage *navBackgroundImage = [UIImage imageNamed:#"navigationbar-ios6"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
}
-(void)customizeios7 {
// SET STATUSBAR TEXT WHITE
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
// SET NAVIGATION IMAGE
UIImage *navBackgroundImage = [UIImage imageNamed:#"navigationbar-ios7"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
}
The image dimensions for your UINavigationBar:
For iOS 6:
640px / 88px
For iOS 7:
640px / 128px
My application has splitview and it's activated only in landscape mode.
In masterview i have tableview and in the last section i have a custom button.
The problem is that the button width stays in potrait instead of using device orientation.
I've tried to play with setAutoresizingMask on button and view but that doesn't work either.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) sectionNum {
if (sectionNum == 4) {
CGRect frameSize;
if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
frameSize = CGRectMake(45.0, 0.0, 600.0, 44.0);
} else {
frameSize = CGRectMake(45.0, 0.0, 680.0, 44.0);
}
UIView *btnView = [[[UIView alloc] initWithFrame:frameSize] autorelease];
// button
UIButton* btnFind = [UIButton buttonWithType:UIButtonTypeCustom];
[btnFind setBackgroundImage:[[UIImage imageNamed:#"buttonblue.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0] forState:UIControlStateNormal];
[btnFind setBackgroundImage:[[UIImage imageNamed:#"buttonred.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0] forState:UIControlStateHighlighted];
btnFind.frame = frameSize;
btnFind.tag = 1;
[btnFind setTitle:#"Find" forState:UIControlStateNormal];
[btnFind addTarget:self action:#selector(doSearchDoc:) forControlEvents:UIControlEventTouchUpInside];
//[btnFind setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
//[btnView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[btnView addSubview:btnFind];
return btnView;
}
return nil;
}
Can someone please let me know how to resolve this.
Thanks.
I've had other issues that the header was not refreshed when rotating the device, ie. the method above was not invoked in my situatio, you could check if this is the case also for you.
The solution I adopted was to invoke reloadData in the rotating method :
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// This solves an issue that the header doesn't show when roating device (ok on simulator)
NSIndexPath* ipselected = [self.tableView indexPathForSelectedRow];
[self.tableView reloadData];
[self.tableView selectRowAtIndexPath:ipselected animated:false scrollPosition:UITableViewScrollPositionMiddle];
}
Of course it is usable if you can afford a full reload when rotating the device but itis typically a time when the user will better accept a 0.5s delay as he/she is busy with moving the device.
lternatively you could keep an ivar to your button and change its frame in the above method.
i have 3 navigation controller and i want to change each background using different image. I was implement a category that extends UINavigationBar like this :
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];}
#end
but it make every navigation bar have a same background image. And then i try to implement code like this :
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[self.navigationController.navigationBar insertSubview:backGroundView atIndex:0];
[backGroundView release];
}
in every controller but each background just show the tintColor, not the image...what should I do???
and how if i want to do that too in tabbarController??
On each of your views, implement the following in your viewWillAppear: method
#import <QuartzCore/QuartzCore.h>
...
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"yourImageBgHere"].CGImage;
}
Cheers,
Rog