SwiftUI wishlist for WWDC20

栏目: IT技术 · 发布时间: 4年前

内容简介:We already started collecting our questions for Apple engineers. On the other hand, I decided to share with you myLast year

We already started collecting our questions for Apple engineers. On the other hand, I decided to share with you my SwiftUI wishlist for WWDC20. This week we will talk about possible additions and changes in SwiftUI . I will show you also API that I expect to see during the next release of SwiftUI .

ScrollView

ScrollView has a bunch of bugs right now. I believe Apple will fix all of them, but I also hope for a content offset binding option. There is no way to get or set the current content offset of ScrollView in SwiftUI . We need a way to both assign and read the offset, and it is a perfect use-case for a Binding . Let’s take a look at the quick example.

struct RootView: View {
    @State private var offset: CGPoint = .zero

    var body: some View {
        ScrollView(.vertical, showsIndicators: false, offset: $offset) {
            Text("Very long text")
            Button("Jump to top") {
                self.offset = .zero
            }
        }
    }
}

CollectionView and CompositionalLayout

Last year Apple released CompositionLayout that provides us a declarative way of building complex layouts for UICollectionView . The buzzword here is declarative . It feels very natural for SwiftUI to have a similar API , I’m not sure why Apple didn’t release it last year, but I expect to see it soon.

CompositionalLayout introduces a few concepts to manage complex layouts. For example, it has sections, groups, and items. As you know, we already have these concepts in SwiftUI . I think SwiftUI views like Group and Section can behave in another way depending on the context. We already saw that views can behave differently inside a Form or a List . Let’s take a look at how it might work in SwiftUI .

struct AppStoreView: View {
    let featured: [App]
    let appsWeLove: [App]

    var body: some View {
        CompositionalLayout {
            Section(.groupPagingCentered) {
                Group(.horizontal, width: .fractionalWidth(0.5), height: .fractionalHeight(0.5)) {
                    ForEach(featured) { app in
                        FeatureAppView(app: app)
                    }
                }
            }

            Section {
                Group(.vertical, width: .fractionalWidth(0.9), height: .estimated(200)) {
                    ForEach(appsWeLove) { app in
                        SmallAppView(app: app)
                    }
                }
            }
        }
    }
}

Navigation

As I already said multiple times, Navigation in SwiftUI is really problematic. The main rule behind the SwiftUI framework is “view is a function of some state” . SwiftUI doesn’t apply this rule to Navigation . Navigation in SwiftUI looks like uncontrollable magic. In my opinion, it also should be a function of a state where the state describes a navigation stack. Let’s take a look at the example.

enum Destination {
    case master
    case details(Post)
}

struct RootView: View {
    @EnvironmentObject var router: Router<Destination>

    var body: some View {
        RouterView(router: router) { destination in
            switch destination {
            case .master:
                return PostsView()
            case .details(let post):
                return PostDetails(post: post)
            }
        }
    }
}

As you can see, RouterView accepts an instance of the Router class, which describes a navigation stack and a ViewBuilder closure that we use to build a view according to the navigation state.

To learn more about @ViewBuilder in SwiftUI , take a look at my “The power of @ViewBuilder in SwiftUI” post.

Appearance API

SwiftUI provides us styling options by introducing a bunch of protocols. For example, ButtonStyle , ToggleStyle , etc. We create a struct that conforms to the ButtonStyle protocol. After that, we can put it into the environment, and all views inside the environment gain particular styling. I appreciate the way styling works in SwiftUI right now, but we need to expand it to support more views like Form and NavigationView .

To learn more about styling in SwiftUI , take a look at my “Composable styling in SwiftUI” post.

Missing views

SwiftUI provides us a set of very basic views that we can use to build our apps. There are a lot of views that we don’t have in SwiftUI . I hope to see a bunch of new views coming in the next weeks. For example, SearchBar , TextView , RefreshControl , etc.

Conclusion

This week, I share my vision and expectations with you about the future of the SwiftUI framework that we hopefully will see later this month. I hope you enjoy the post. Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading, and see you next week!


以上所述就是小编给大家介绍的《SwiftUI wishlist for WWDC20》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

深入理解C#(第3版)

深入理解C#(第3版)

斯基特 (Jon Skeet) / 姚琪琳 / 人民邮电出版社 / 2014-4-1 / 99.00元

本书是世界顶级技术专家“十年磨一剑”的经典之作,在C#和.NET领域享有盛誉。与其他泛泛介绍C#的书籍不同,本书深度探究C#的特性,并结合技术发展,引领读者深入C#的时空。作者从语言设计的动机出发,介绍支持这些特性的核心概念。作者将新的语言特性放在C#语言发展的背景之上,用极富实际意义的示例,向读者展示编写代码和设计解决方案的最佳方式。同时作者将多年的C#开发经验与读者分享,读者可咀其精华、免走弯......一起来看看 《深入理解C#(第3版)》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具