我有一个 SwiftUI 视图,我使用UIHostingController
. 问题是 SwiftUI 视图的背景颜色在托管在UIHostingController
.
var body: some View {
NavigationView {
VStack(spacing: 20) {
VStack {
Button {
print("Hello")
DispatchQueue.main.async { mainStore.dispatch(goToHomePage()) }
} label: {
Image("Logo")
.frame(width: 120, height: 120)
.background(SwiftUI.Color("brand"))
.clipShape(Circle())
.frame(maxWidth: .infinity)
.background(
VStack(spacing: 0) {
Rectangle().fill(SwiftUI.Color("greyExtraLight"))
Rectangle().fill(SwiftUI.Color.white)
}
)
}
Text(name)
.font(.custom("FoundersGrotesk-Medium", size: 20))
.padding(.top, 15)
.foregroundColor(.black)
Text(email)
.font(.custom("FoundersGrotesk-Regular", size: 14))
.padding(.top, 1)
.foregroundColor(SwiftUI.Color("greyDim"))
ForEach(profileSectionTexts, id: \.self) { text in
Button {
return
} label: {
HStack(alignment: .center) {
Text(text)
.font(.custom("FoundersGrotesk-Regular", size: 15))
.foregroundColor(SwiftUI.Color("greyDim"))
Spacer()
Image(systemName: "chevron.forward")
.font(.system(size: 12))
.foregroundColor(SwiftUI.Color("greyDim"))
}
}
.frame(height: 42)
}
.padding(.leading, 18)
.padding(.trailing, 18)
}
.padding(.bottom, 18)
.background(SwiftUI.Color.white)
.cornerRadius(4)
VStack(alignment: .leading) {
Text("Scans")
.font(.custom("FoundersGrotesk-Medium", size: 20))
.foregroundColor(.black)
.padding(.top, 9)
ForEach(scansSectionTexts, id: \.self) { text in
NavigationLink(destination: EditProfilePage(name: "John Doe", email: "john@xyz.com")) {
Button {
return
} label: {
HStack(alignment: .center) {
Text(text)
.font(.custom("FoundersGrotesk-Regular", size: 15))
.foregroundColor(SwiftUI.Color("greyDim"))
Spacer()
Image(systemName: "chevron.forward")
.font(.system(size: 12))
.foregroundColor(SwiftUI.Color("greyDim"))
}
}
.frame(height: 42)
}
}
}
.padding(18)
.background(SwiftUI.Color.white)
.cornerRadius(4)
VStack(alignment: .leading) {
Text("About")
.font(.custom("FoundersGrotesk-Medium", size: 20))
.foregroundColor(.black)
.padding(.top, 9)
ForEach(aboutSectionTexts, id: \.self) { text in
Button {
return
} label: {
HStack(alignment: .center) {
Text(text)
.font(.custom("FoundersGrotesk-Regular", size: 15))
.foregroundColor(SwiftUI.Color("greyDim"))
Spacer()
Image(systemName: "chevron.forward")
.font(.system(size: 12))
.foregroundColor(SwiftUI.Color("greyDim"))
}
}
.frame(height: 42)
}
}
.padding(18)
.background(SwiftUI.Color.white)
.cornerRadius(4)
Button("Logout") {
return
}
.font(.custom("FoundersGrotesk-Regular", size: 14))
.foregroundColor(.black)
.padding(18)
.frame(maxWidth: .infinity, maxHeight: 42)
.background(SwiftUI.Color.white)
.cornerRadius(4)
}
.padding(18)
.padding(.top, 20)
.background(SwiftUI.Color("greyExtraLight"))
.edgesIgnoringSafeArea(.all)
.navigationBarItems(leading: Button(action: {
self.presentationMode.wrappedValue.dismiss()
DispatchQueue.main.async {
mainStore.dispatch(goToHomePage())
}
}) {
Image(systemName: "chevron.backward")
.font(Font.system(size: 15, weight: .medium))
.foregroundColor(SwiftUI.Color("greyDim"))
})
}
}
这就是我在以下位置托管 SwiftUI 视图所做的工作UIHostingController
:
let profilePage = UIHostingController(rootView: ProfilePage(name: "John Doe", email: "john@xyz.com"))
profilePage.view.backgroundColor = .clear
addChildViewController(profilePage)
view.addSubview(profilePage.view)
profilePage.didMove(toParentViewController: self)
profilePage.view.translatesAutoresizingMaskIntoConstraints = false
profilePage.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
profilePage.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
profilePage.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
profilePage.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
左边是它应该的样子,右边是它现在的样子。
感谢您的帮助!