简体   繁体   English

仅使用后退按钮和透明背景导航

[英]Navigation with only Back Button and transparent background

I had tried to implement Navigation Controller which embeds in my View Controller. 我曾尝试实现嵌入我的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 . 有没有办法克服这种情况。

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 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

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. 我用UIView创建了自定义.XIB。 and Add subview it to as Navigationbar. 并将子视图添加到导航栏。

So, According to me the best option is , create custom .XIB with Height of UINavigation bar. 所以,据我所知,最好的选择是,用UINavigation栏的高度创建自定义.XIB。 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 1)获取大小为宽度46高度44的 .h文件, .m文件和UIView的Xib

2) Set constrain and layout according to your suitable design. 2)根据您的设计设置约束和布局。

3) Give outlet to Back button in .h file. 3)在.h文件中为“后退”按钮添加插座。

4) Create one delegate in customview.h file . 4)在customview.h文件中创建一个委托。 set method in .m file. 在.m文件中设置方法。

5) then import customview.h file in your viewcontroller.h file . 5)然后在viewcontroller.h文件中导入customview.h文件。 As same don't forget to define delegate in viewcontrollers interface. 同样不要忘记在viewcontrollers接口中定义委托。 <> <>

6) Now add customheaderview as subview in your Viewdidload method. 6)现在在Viewdidload方法中添加customheaderview作为子视图。

#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. 7)使用headerview的委托方法在所有视图控制器中访问它。

Now you can do anything in navigationbar like you do in UIView. 现在,您可以像在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 . 但是当你在导航栏中需要3或4个按钮并在其上设置动作时,它是值得的。 or need same design layout for whole project in navigationbar.. 或者需要在导航栏中为整个项目设计相同的布局..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM