有没有相当于 SwiftUI 的 zstack 的 UIKit?
我正在尝试创建类似 this 的内容。我最近一直在使用 SwiftUI,所以我知道我可以通过向 zstack 添加图像、文本和按钮(我是灵活的文本是按钮/NavigationLink 的标签)来创建它。但我正在四处寻找,看看是否有办法在 UIKit 中做到这一点。最好不使用故事板。我对 cocoapods 库或其他库持开放态度,如果需要的话。我环顾四周并探索了使用 SwiftUI 创建所需的 ZStack,然后在我的 UIKit 中使用 UIHostingController,但因为它涉及按钮/导航链接。鉴于 NavigationLink 如何要求目的地符合视图,我想在将我的项目的更多内容转换为 swiftui 之前询问一下。我更希望这个项目能给我更多在 UIKit 中构建视图而无需故事板的经验,所以我更愿意这样做而不是使用 SwiftUI。如果可能的话我想。
我尝试过四处搜索,但所有涉及 UIButtons 和图像的 Google 搜索都只是链接到有关在 UIButton 中设置图像的帖子。
I'm trying to create something like this. I've been working with SwiftUI recently so I know I could create that by adding an image, text and button (the I'm flexible text is the label for a button/NavigationLink) to a zstack. but I'm looking around trying to see if there's anyway to do that in UIKit. preferably without using storyboards. I'm open to a cocoapods library or whatever if that's what it takes. I've looked around and explored using SwiftUI to create the desired ZStack and then use it in my UIKit with a UIHostingController but because it involves a button/navigationlink. seeing as how the NavigationLink would require the destination to conform to a View, I wanted to ask around before converting even more of my project to swiftui. I was more hoping this project would be for giving me more experience building views in UIKit without storyboards so I'd prefer to do that instead of using SwiftUI. if that's possible I guess.
I've tried searching around but all my google searches involving UIButtons and images just link to posts about setting the image in a UIButton.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您希望获得更多使用
UIKit
创建视图的经验,因此我创建了一个继承自UIView
的视图,您可以重用该视图。在 UIKit 中有相当多的代码可以得到相同的结果。下面提供了代码和输出。注意:阅读提供的注释
代码
输出
重要提示
您可以使用约束或框架创建布局。如果您使用约束,请将视图
.translatesAutoresizingMaskIntoConstraints
设置为 false 重要(您可以阅读其文档)。NSLayoutConstraint.activate([...])
用于一次应用一组约束。或者,您可以使用:对于单独的约束
例如,从前缘/左边缘,您需要向视图中心添加 10 的填充,或从右/尾侧向中心添加 -10 的填充。
since you wanted to get more experience in creating views using
UIKit
, I've created a view that inherits fromUIView
that you can reuse. There's quite a lot of code to get the same result in UIKit. The code and output are provided below.NOTE: Read the comments provided
Code
Output
Important pointers
You can create the layout using constraints or frames. In case you are using constraints, it is important to set a views
.translatesAutoresizingMaskIntoConstraints
to false (You can read the documentation for it).NSLayoutConstraint.activate([...])
Is used to apply an array of constraints at once. Alternatively, you can use:for individual constraints
E.x., From the leading/left edge, you will need to add a padding of 10 towards the centre of the view or -10 from the right/trailing side towards the centre.