I am trying to create a transparent UINavigationBar
and here is what I do:
First, I have a navVC and a general VC as root VC in my code.
SNLoginViewController *loginVC = [[SNLoginViewController alloc] init];
SNLoginNavController *loginNavVC = [[SNLoginNavController alloc] initWithRootViewController:loginVC];
[self presentViewController:loginNavVC animated:YES completion:nil];
I didn't override the init
method of SNLoginViewController
.
And here is the implementation of initWithRootViewController:
:
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:rootViewController];
if (self) {
UIImage *bgImage = [UIImage imageNamed:@"bg"];
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgImage];
bgImageView.frame = [UIScreen mainScreen].bounds;
[self.view insertSubview:bgImageView atIndex:0];
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"transparent"]//transparent is a transparent png
forBarMetrics:UIBarMetricsCompact];
self.navigationBar.shadowImage = [UIImage imageNamed:@"transparent"];
self.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:18]}];
self.navigationBar.clipsToBounds = YES;
}
return self;
}
Using these codes (setBackgroundImage:forBarMetrics:
) I should have been able to make the navigation bar transparent but something went wrong. The major problem is that somehow the frame of loginVC
is smaller than loginNavVC
.
(source: zybuluo.com)
In the figure above the selected view is of loginVC
and the one on the left is of loginNavVc
. loginVC
is smaller than loginNavVc
.
But in viewDidLoad
of loginVC
I print the selected view, its frame is (0 0; 320 568) but when I print its description in view hierarchy (same address in memory), its frame is (0 64; 320 504). Why would this happen? How to make it full screen(not full screen like games, status bar should still be visiable)?
Add this before presenting your navigation controller.
[self.loginNavVC.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self. loginNavVC.navigationBar.translucent = YES;