swift - 导航栏中的后退按钮重叠,快速 4
问题描述
我试图根据本教程自定义我的后退按钮。在 AppDelegate 中,
let barButtonAppearence = UIBarButtonItem.appearance()
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let backButton = UIImage(named: "back_arrow")
let backButtonImage = backButton?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 10)
barButtonAppearence.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
return true
}
然后它与现有的冲突(由于segue(Show)而自动出现。
所以我需要删除蓝色的。
解决方案
有两件事需要完成你想要达到的目标:
- 将后退按钮的默认图像更改为您提供的图像
- 从后退按钮项目中删除标题
要更改返回按钮的图像:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Remember, this will change every navigation bar's back button image in your app
UINavigationBar.appearance().backIndicatorImage = #imageLiteral(resourceName: "backButton")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = #imageLiteral(resourceName: "backButton")
return true
}
注意:如果您不希望所有导航栏后退按钮都具有提供的图像,则可能需要子类化UINavigationController
并更新其导航栏。
从后退按钮项中删除标题:
我们将通过向 any UIViewController
through 扩展添加一个方法来做到这一点。此处将使用扩展方法,以便任何人UIViewController
都可以在需要时具有此行为。
extension UIViewController {
func removeNavigationBarBackButtonItemTitle() {
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
}
}
现在,在VC A -> VC B的任何推送转换中,您需要隐藏后退按钮的标题。但是您必须removeNavigationBarBackButtonItemTitle()
从VC A调用该方法。只要记住这一点,你就可以开始了。
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
removeNavigationBarBackButtonItemTitle()
}
}
你会在这里找到一个演示。该演示也有一些其他的实现。但是你会得到你需要的和我上面所说的。
推荐阅读
- angular - 如果产品已经在 localStorage 中,则不要添加产品
- reactjs - 无法解析“反应路由器”
- server - 在服务器模式下启动 GSM 调制解调器时如何获取公共 IP 地址?
- excel - 跨多个工作表求和时忽略隐藏工作表
- .htaccess - 如何在不影响根目录索引页的情况下从 url 隐藏子目录
- javascript - JavaScript通过innerText罢工不起作用
- swift - 创建一个“新建”按钮,在 Swift 中创建 3 个 UI 文本字段
- javascript - function.promise.then() 和 function.then() 的区别
- android - 我无法使用 oauth 2.0 与我的服务器使用 android 连接
- typescript - 打字稿中的“鸭子”打字与函数参数