- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 SwiftUI 中创建一个加载指示器,它应该始终位于 View 层次结构的顶级 View 的中心(即在全屏应用程序中位于整个屏幕的中心)。这在 UIKit 中很容易,但 SwiftUI 仅将 View 相对于它们的父 View 居中,我无法获得父 View 的父 View 的位置。
遗憾的是,我的应用程序并非完全基于 SwiftUI,因此我无法轻松地在我的 Root View 上设置属性,然后我可以在我的加载 View 中访问这些属性——我需要这个 View 居中,无论 View 层次结构是什么样的(混合 UIKit - SwiftUI 父 View )。这就是为什么像 SwiftUI set position to centre of different view 这样的答案不适用于我的用例,因为在该示例中,您需要修改要将 subview 居中的 View 。
我试过使用 View
的 .offset
和 .position
函数,但是,我无法获得正确的输入始终动态地居中我的 loadingView
而不管屏幕大小或整个屏幕的哪个部分 rootView
占据。
请在下面找到问题的最小可重现示例:
/// Loading view that should always be centered in the whole screen on the XY axis and should be the top view in the Z axis
struct CenteredLoadingView<RootView: View>: View {
private let rootView: RootView
init(rootView: RootView) {
self.rootView = rootView
}
var body: some View {
ZStack {
rootView
loadingView
}
// Ensure that `AnimatedLoadingView` is displayed above all other views, whose `zIndex` would be higher than `rootView`'s by default
.zIndex(.infinity)
}
private var loadingView: some View {
VStack {
Color.white
.frame(width: 48, height: 72)
Text("Loading")
.foregroundColor(.white)
}
.frame(width: 142, height: 142)
.background(Color.primary.opacity(0.7))
.cornerRadius(10)
}
}
应在其上方显示加载 View 的 View :
struct CenterView: View {
var body: some View {
return VStack {
Color.gray
HStack {
CenteredLoadingView(rootView: list)
otherList
}
}
}
var list: some View {
List {
ForEach(1..<6) {
Text($0.description)
}
}
}
var otherList: some View {
List {
ForEach(6..<11) {
Text($0.description)
}
}
}
}
我尝试使用 GeometryReader
和 .frame(in: .global)
修改 CenteredLoadingView
的 body
> 以获得全局屏幕尺寸,但我所取得的成就是现在我的 loadingView
根本不可见。
var body: some View {
GeometryReader<AnyView> { geo in
let screen = geo.frame(in: .global)
let stack = ZStack {
self.rootView
self.loadingView
.position(x: screen.midX, y: screen.midY)
// Offset doesn't work either
//.offset(x: -screen.origin.x, y: -screen.origin.y)
}
// Ensure that `AnimatedLoadingView` is displayed above all other views, whose `zIndex` would be higher than `rootView`'s by default
.zIndex(.infinity)
return AnyView(stack)
}
}
最佳答案
这是一个可能的方法演示。这个想法是使用注入(inject)的 UIView 来访问 UIWindow,然后将加载 View 显示为窗口的 Root View Controller View 的顶 View 。
使用 Xcode 12/iOS 14 测试(但兼容 SwiftUI 1.0)
注意:动画、效果等是可能的,但为简单起见超出范围
struct CenteredLoadingView<RootView: View>: View {
private let rootView: RootView
@Binding var isActive: Bool
init(rootView: RootView, isActive: Binding<Bool>) {
self.rootView = rootView
self._isActive = isActive
}
var body: some View {
rootView
.background(Activator(showLoading: $isActive))
}
struct Activator: UIViewRepresentable {
@Binding var showLoading: Bool
@State private var myWindow: UIWindow? = nil
func makeUIView(context: Context) -> UIView {
let view = UIView()
DispatchQueue.main.async {
self.myWindow = view.window
}
return view
}
func updateUIView(_ uiView: UIView, context: Context) {
guard let holder = myWindow?.rootViewController?.view else { return }
if showLoading && context.coordinator.controller == nil {
context.coordinator.controller = UIHostingController(rootView: loadingView)
let view = context.coordinator.controller!.view
view?.backgroundColor = UIColor.black.withAlphaComponent(0.8)
view?.translatesAutoresizingMaskIntoConstraints = false
holder.addSubview(view!)
holder.isUserInteractionEnabled = false
view?.leadingAnchor.constraint(equalTo: holder.leadingAnchor).isActive = true
view?.trailingAnchor.constraint(equalTo: holder.trailingAnchor).isActive = true
view?.topAnchor.constraint(equalTo: holder.topAnchor).isActive = true
view?.bottomAnchor.constraint(equalTo: holder.bottomAnchor).isActive = true
} else if !showLoading {
context.coordinator.controller?.view.removeFromSuperview()
context.coordinator.controller = nil
holder.isUserInteractionEnabled = true
}
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator {
var controller: UIViewController? = nil
}
private var loadingView: some View {
VStack {
Color.white
.frame(width: 48, height: 72)
Text("Loading")
.foregroundColor(.white)
}
.frame(width: 142, height: 142)
.background(Color.primary.opacity(0.7))
.cornerRadius(10)
}
}
}
struct CenterView: View {
@State private var isLoading = false
var body: some View {
return VStack {
Color.gray
HStack {
CenteredLoadingView(rootView: list, isActive: $isLoading)
otherList
}
Button("Demo", action: load)
}
.onAppear(perform: load)
}
func load() {
self.isLoading = true
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.isLoading = false
}
}
var list: some View {
List {
ForEach(1..<6) {
Text($0.description)
}
}
}
var otherList: some View {
List {
ForEach(6..<11) {
Text($0.description)
}
}
}
}
关于ios - 在顶级 View 中居中 SwiftUI View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63251367/
我有一个用于重构练习的测试套件,我希望它与 Data.List 兼容。和 Data.List.NonEmpty .练习包含一个函数 foo :: [Foo] -> Foo并且测试套件有一些 data
我下载了 Android Studio 项目。项目根目录只有一个 build.gradle 具有下一个结构: apply plugin: 'com.android.library' android {
我似乎无法正确使用最接近的函数来删除删除按钮之外的 UL。 HTML X Data Jquery $('.remove').click(function(){
我需要一些帮助。顶级 Activity 必须位于自定义包之外吗?这样的话,MainActivity应该是在自定义包之外吧?Activity 是自定义包名称。
我正在尝试使用 AppCompat Drawer 和多个顶级 Activity (不是 fragment )创建一个基本应用程序 - 我不太清楚如何管理后台堆栈 - 我已经尝试了大约一百种不同的方法-
无法找到任何相关信息,我可以自己完成此操作,但我觉得将其保留在查询中可能是最好的选择(如果可能的话)。 基本上我想尝试添加查询的顶级“统计”部分。 所以当我得到结果时,我会看到这样的结果 num_ro
我在菜单中遇到 onmouseout/over delay 问题。我发现通过将 setTimeout 数字从 100 更改为 2000,它延迟了顶层菜单的隐藏而不是子级菜单,并且在新的 onmouse
是否有一个选项可以通过使用动画在运行时简单地 move Tkinter TopLevel() 窗口?我想到了由按钮触发的平滑 moveInAnimation。 这是一些代码片段: from Tkint
我从正在调用的 API 中获取了以下 JSON 文件: { "14500": [ { "5": { "versionName": "VersionOne",
在 C++ Primer 第五版中,它说: int f(int){ /* can write to parameter */} int f(const int){ /* cannot write to
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在使用 Retrofit 进行一些 API 调用。对于特定端点,返回的 json 看起来有点像这样: 端点:api.example.com/1.0/userinfo?userid=7 返回的响应看
假设我有一个 Python 文件,我想在顶层运行它,但在它完成后,我想从它停止的地方继续。我希望能够使用它创建的对象等。 一个简单的例子,假设我有一个执行 i = 5 的 Python 脚本。当脚本结
我在 Xcode 中使用 Cmake 来生成一个 c++/c“项目”(my_project) 和一些 c++/c“目标”(一个是二进制文件,其余是库) 我的 CMakeLists.txt 看起来像这样
我正在尝试学习 redux,但遇到了错误。我只有两个文件,一个 index.html 文件和一个 main.js 文件,html 文件中有指向 jquery 和 redux cdns 的链接。我在 r
我有这个通用函数可以保存在 NSUserDefaults 中,通常可以正常工作,但现在我想保存一个 boolean 值,但出现错误。我找不到任何东西,我不明白为什么它不起作用。 extension U
我正在使用 Retrofit 进行 API 调用。顶级对象的命名取决于请求的查询参数。例如,这样的请求: api.somewebsite.com/1.0/mix_info?mix_id=69 返回这样
我做汤: from bs4 import BeautifulSoup soup = BeautifulSoup("My paragraph My link", "html.parser") 我想删除第
看完new egghead course由 Dan Abramov 撰写,我对提到的选择器有疑问。 选择器的目的是向组件隐藏状态树的详细信息,以便在树发生变化时便于以后管理代码。 如果我理解正确,那意
我是一名优秀的程序员,十分优秀!