![](/img/trans.png)
[英]Swift & Navigation : Navigation Bar changes its background color when scroll the view
[英]Navigation bar only have background when scroll
我正在嘗試實現從導航視圖鏈接的詳細視圖。 在此詳細視圖中,頂部有一個默認導航欄,帶有一個后退按鈕。 但是當我向上滾動時,該欄只顯示一些顏色。 我不知道為什么。
最初,導航欄在滾動或不滾動時都沒有背景。 所以我創建了一個 init() 方法來設置樣式。
init(fruit: Fruit) {
self.fruit = fruit
if #available(iOS 15.0, *) {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
}
}
Body View 是外部的導航視圖和內部的滾動視圖。 ** 有人想知道為什么我將導航欄設置為隱藏在 VStack 中,是因為如果我不隱藏它,圖像上方會有一些巨大的空間。 (我不知道為什么)
** 更新代碼 ** 我更新了使用不透明背景的代碼。 但似乎這些配置都不可見。
init(fruit: Fruit) {
self.fruit = fruit
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance
UINavigationBar.appearance().standardAppearance = navBarAppearance
}
var body: some View {
NavigationView {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .center, spacing: 20) {
// HEADER
FruitHeaderView(fruit: fruit)
VStack(alignment: .leading, spacing: 20) {
// TITLE
Text(fruit.title)
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(fruit.gradientColors[1])
} //: VSTACK
.padding(.horizontal, 20)
.frame(maxWidth: 640, alignment: .center)
} //: VSTACK
.navigationBarHidden(true)
} //: SCROLL
.edgesIgnoringSafeArea(.top)
} //: NAVIGATION
.navigationBarTitle(fruit.title, displayMode: .inline)
.navigationViewStyle(StackNavigationViewStyle())
}
***解決方案***原來我必須將導航欄的配置代碼放在父視圖中。 在 init() 期間。 誰能在父視圖上解釋為什么會這樣? 或者,如果我想要父母和孩子的風格不同,我該怎么辦?
init() {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance
UINavigationBar.appearance().standardAppearance = navBarAppearance
}
var body: some View {
NavigationView {
List {
ForEach(fruits.shuffled()) { item in
NavigationLink {
FruitDetailView(fruit: item)
} label: {
FruitRowView(fruit: item)
.padding(.vertical, 4)
}
}
}
.listStyle(.plain)
.navigationTitle("Fruits")
} //: NAVIGATION
}
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
在 viewWillAppear 中使用此代碼
使用我的擴展:
extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.backgroundColor = backgoundColor
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.compactAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.tintColor = tintColor
navigationItem.title = title
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = backgoundColor
navigationController?.navigationBar.tintColor = tintColor
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = title
}
}
}
在 viewDidAppear 或 viewDidLoad 中調用它,在您的情況下將背景設置為清除... 如何使用:
configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "YourTitle", preferredLargeTitle: false)
您需要不透明的外觀,因為default
的一種表示system-selected-by-design
,它從一個版本更改為另一個版本。
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance
使用 Xcode 13.3 / iOS 15.4 測試(在 ContentView.init 中)
注意:onAppear 來不及注入上面的代碼,外觀設置應用在它之后創建的對象上,並且在 NavigationView 創建之后調用 onAppear。 因此,在 init 或其他任何地方使用它,但在創建視圖之前。
這對我有用 -
func changeNavBar(navigationBar: UINavigationBar, to color: UIColor) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = color
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.