你如何使UINavigationBar透明?虽然我希望它的酒吧项目仍然可见.
重现问题的前提条件:
在实际设备中,状态栏将在绿色导航栏上方保留为白色。
我尝试的解决方案:
有任何想法吗?感谢您的帮助。
我有一个像这样的简单课程.
public class User {
let id: Int
let firstName: String
let lastName: String
let email: String?
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
Run Code Online (Sandbox Code Playgroud)
这在以前的Swift版本中编译得很好.在Swift 1.2中,我得到以下编译错误.
从初始化程序返回而不初始化所有存储的属性
为什么这样,我该如何解决?
如何在iOS 11中为大型标题NavigationBar设置自定义背景图像?我正在使用我已分配给故事板中的navigationControllers的自定义子类.
这是我创建自定义NavBar的方法:
class CustomNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationBar.tintColor = UIColor(red:1, green:1, blue:1, alpha:0.6)
self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
if #available(iOS 11.0, *) {
self.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .automatic
self.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
self.navigationBar.barTintColor = UIColor.green
}
self.navigationBar.isTranslucent = false
self.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "navigationBarBackground"), for: .default)
self.navigationBar.shadowImage = #imageLiteral(resourceName: "navigationBarShadow")
}
}
Run Code Online (Sandbox Code Playgroud)
奇怪的setBackgroundImage(image, for: .default)是,这对大型游戏不起作用.以前在iOS 10上工作过,如果我旋转iPhone(并激活小型NavBar)背景又回来了吗?
编辑:
在backgroundImage仍然呈现,但不知隐藏.仅当您开始滚动并出现"普通"导航栏时,才会看到backgroundImage.另外,barTintColor完全是在这种情况下被忽略.

我刚刚开始在 SwiftUI 中编码并遇到了一个问题。我需要为导航栏(NavigationView)的背景赋予不同的颜色。当我从一个视图转到下一个视图时,颜色会发生变化。我需要让它为“内联”的 navigationBarTitleDisplayMode 工作。
我尝试了SwiftUI update navigation bar title color中提出的解决方案 ,但这些解决方案都不能完全满足我的需要。
对该帖子的回复中的解决方案适用于内联: 使用 UIViewControllerRepresentable。然而,当我们第一次打开视图时,它会显示前一个视图的颜色一秒钟,然后再更改为新颜色。我想避免这种情况,并在所有内容出现在屏幕上时立即显示颜色。有没有办法做到这一点?
这个其他解决方案也不起作用:在 init() 中更改 UINavigation 的外观,因为当我在 init() 中设置背景时,它将更改应用程序中所有视图的背景。同样,我需要视图具有不同的背景颜色。
我尝试了类似于此解决方案的方法:修改工具栏,但它不允许我更改导航栏的颜色。
我尝试的另一个解决方案是:Creating navigationBarColor function,它基于:NAVIGATIONVIEW DYNAMIC Behind COLOR IN SWIFTUI。该解决方案适用于navigationBarTitleDisplayMode“large”,但是当将navigationBarTitleDisplayMode设置为“inline”时,它将以不同的颜色显示导航栏的背景颜色,就好像它被灰色/透明层覆盖一样。例如,它在“大”模式下显示的颜色是: 大模式下的红色 但是相反,它显示的颜色是: 内联模式下的红色
最后,我尝试了这个解决方案:子类化 UIViewController 并配置 viewDidLayoutSubviews(),但它也无法满足我的需求。
最接近我需要的解决方案是 1. 和 4.,但它们仍然不能 100% 工作。
有人知道如何使这些解决方案中的任何一个都适用于内联的 navigationBarTitleDisplayMode,能够更改不同布局中导航栏的背景颜色,并在显示视图后显示新颜色(无延迟)?
谢谢你!
顺便说一下,我使用的是 XCode 12.5。
以下是我正在使用的示例代码,以示例 4. 作为模型:
FirstView.swift
import SwiftUI
struct FirstView: View {
@State private var selection: String? = nil
var …Run Code Online (Sandbox Code Playgroud) Swift 支持隐藏滚动条上的导航栏navigationController?.hidesBarsOnSwipe = true
需要明确的是,我希望它只隐藏在滚动条上,所以.navigationBarHidden(true)还不够。
我尝试按照 Stackoverflow 答案中的描述访问 NavigationController (我添加了nc.hidesBarsOnSwipe = true),但在编译时,它不起作用。
SwiftUI 支持吗?
我正在寻找一种使 NavigationBar 透明的方法。我的 NavigationView 位于 ContentView 的根视图中,其中包含一个 TabView。
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
HomeView().tabItem {
Image(systemName: "house.fill")
Text("Home")
}.tag(1)
NavigationView {
SearchView()
}
.tabItem {
Image(systemName: "magnifyingglass")
Text("Search")
}.tag(2)
}
Run Code Online (Sandbox Code Playgroud)
即使在根视图中添加以下修饰符后,导航视图栏也会显示。
init() {
UINavigationBar.appearance().backgroundColor = .clear
UINavigationBar.appearance().isHidden = false
Run Code Online (Sandbox Code Playgroud)
}
下面是我试图隐藏导航栏背景的子视图。
import SwiftUI
struct FacilityView: View {
var perks = "Badge_NoPerks"
var image = "Image_Course6"
var courseName = "Course"
var body: some View {
VStack {
HStack {
Image(image)
.resizable()
.aspectRatio(contentMode: .fill) …Run Code Online (Sandbox Code Playgroud) 在上图中,id 喜欢更改-02:49为一种颜色,例如Color.blue
我试过了:
struct ContentView: View {
var body: some View {
PlayerView().accentColor(Color.blue)
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试将它添加到实际的 PlayerView 中,如下所示:
struct PlayerView: View {
var body: some View {
VStack{
....
}.navigationBarTitle(Text(“-2:49”))
.accentColor(.blue)
}
}
Run Code Online (Sandbox Code Playgroud)
我也试过:
struct PlayerView: View {
var body: some View {
VStack{
....
}.navigationBarTitle(Text(“-2:49”).foregroundColor(.blue))
}
}
Run Code Online (Sandbox Code Playgroud) ios ×7
swiftui ×6
swift ×4
background ×1
class ×1
inline ×1
ios11 ×1
ios13 ×1
iphone ×1
navigation ×1
objective-c ×1
storyboard ×1
transparency ×1
uistoryboard ×1
watchos-6 ×1
xcode ×1