Navigation with only Back Button and transparent b

2019-04-11 12:13发布

问题:

I had tried to implement Navigation Controller which embeds in my View Controller. It works as expected.

But my requirement is slightly different which needs only a back button and already have a top banner with logo image background in all the screens . So if I try to implement back button it takes the space for the navigation bar which covers the Logo/top banner.

Is there any way to overcome this scenario .

回答1:

That seems pretty easy, by the following code used in my app too:-

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
self.navigationController?.view.backgroundColor = UIColor.clearColor()

Edit:-

To remove back button text:-

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)

Swift 3.0

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear


回答2:

I don't know is there any other option or not. But in the past i have same scenario as you.

I have created custom .XIB with UIView. and Add subview it to as Navigationbar.

So, According to me the best option is , create custom .XIB with Height of UINavigation bar. and Add as subview.

Here is step how you can achieve it.

1) Take .h file , .m file and Xib of UIView with size of width 46 and height 44

2) Set constrain and layout according to your suitable design.

3) Give outlet to Back button in .h file.

4) Create one delegate in customview.h file . set method in .m file.

5) then import customview.h file in your viewcontroller.h file . As same don't forget to define delegate in viewcontrollers interface. <>

6) Now add customheaderview as subview in your Viewdidload method.

#define macro_name (Define in constantfile as macro if you don't want to write whole code again and again)

NavigationHeaderview *customView = [NavigationHeaderview CustomNavigation:self.navigationController.navigationBar.frame.origin.x y:self.navigationController.navigationBar.frame.origin.y width:self.navigationController.navigationBar.frame.size.width height:self.navigationController.navigationBar.frame.size.height];
customView.delegate=self;
[self.navigationController.view addSubview:customView];

7) Access it in your all view controller with delegate method of headerview.

Now you can do anything in navigationbar like you do in UIView.

Note:- I know it is tedious process. but it is worth when you need 3 or 4 button in navigation bar and set Action on it . or need same design layout for whole project in navigationbar..