536 Stimmen

Wie man die 1px untere Linie der UINavigationBar ausblendet

Ich habe eine App, bei der die Navigationsleiste manchmal mit dem Inhalt verschmelzen muss.

Weiß jemand, wie man diese lästige kleine Leiste loswerden oder ihre Farbe ändern kann?

Auf dem Bild unten habe ich die Situation - ich spreche von dieser 1px hohen Linie unter "Root View Controller"

Bildbeschreibung hier eingeben

2voto

Reza Shirazian Punkte 2121

Antwort von pxpgraphics für Swift 3.0.

import Foundation
import UIKit

extension UINavigationBar {

  func hideBottomHairline() {
    let navigationBarImageView = hairlineImageViewInNavigationBar(view: self)
    navigationBarImageView!.isHidden = true
  }

  func showBottomHairline() {
    let navigationBarImageView = hairlineImageViewInNavigationBar(view: self)
    navigationBarImageView!.isHidden = false
  }

  private func hairlineImageViewInNavigationBar(view: UIView) -> UIImageView? {
    if view is UIImageView && view.bounds.height <= 1.0 {
      return (view as! UIImageView)
    }

    let subviews = (view.subviews as [UIView])
    for subview: UIView in subviews {
      if let imageView: UIImageView = hairlineImageViewInNavigationBar(view: subview) {
        return imageView
      }
    }
    return nil
  }
}

extension UIToolbar {

  func hideHairline() {
    let navigationBarImageView = hairlineImageViewInToolbar(view: self)
    navigationBarImageView!.isHidden = true
  }

  func showHairline() {
    let navigationBarImageView = hairlineImageViewInToolbar(view: self)
    navigationBarImageView!.isHidden = false
  }

  private func hairlineImageViewInToolbar(view: UIView) -> UIImageView? {
    if view is UIImageView && view.bounds.height <= 1.0 {
      return (view as! UIImageView)
    }

    let subviews = (view.subviews as [UIView])
    for subview: UIView in subviews {
      if let imageView: UIImageView = hairlineImageViewInToolbar(view: subview) {
        return imageView
      }
    }
    return nil
  }
}

2voto

Socheat Punkte 283

Sie sollten eine Ansicht am unteren Rand des UISearchBar hinzufügen

let rect = searchController.searchBar.frame;
let lineView : UIView = UIView.init(frame: CGRect.init(x: 0, y: rect.size.height-1, width: rect.size.width, height: 1))
lineView.backgroundColor = UIColor.init(hexString: "8CC73E")
searchController.searchBar.addSubview(lineView)

2voto

Léo Natan Punkte 56332

Das Problem beim Einstellen eines Hintergrundbildes besteht darin, dass es das Verschwimmen entfernt. Sie können es entfernen, ohne ein Hintergrundbild festzulegen. Sehen Sie meine Antwort hier.

1voto

Karthik damodara Punkte 1731

In Swift 3 machen wir es so

Für jeden ViewController:

navigationBar.shadowImage = UIImage()
setBackgroundImage(UIImage(), for: .default)

Für eine gesamte App:

UINavigationBar.appearance().setBackgroundImage(UIImage(),barMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()

1voto

BatyrCan Punkte 5575
        if #verfügbar(iOS 13.0, *) {
            let aussehen = UINavigationBarAppearance()
            aussehen.backgroundColor          = Colors.color_app
            aussehen.titleTextAttributes      = [.foregroundColor : UIColor.white]
            aussehen.largeTitleTextAttributes = [.foregroundColor : UIColor.white]
            aussehen.shadowColor = .clear
            aussehen.shadowImage = UIImage()

            UINavigationBar.appearance().tintColor            = .white
            UINavigationBar.appearance().standardAppearance   = aussehen
            UINavigationBar.appearance().compactAppearance    = aussehen
            UINavigationBar.appearance().scrollEdgeAppearance = aussehen
        } else {
            UINavigationBar.appearance().barTintColor        = Colors.color_app
            UINavigationBar.appearance().tintColor           = .white
            UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
            if #verfügbar(iOS 11.0, *) {
                UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            }
            UINavigationBar.appearance().isTranslucent = false

            UINavigationBar.appearance().shadowImage = UIImage()
            UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
        }

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X