肇鑫的技术博客

业精于勤,荒于嬉

SwiftUI下,TextField诡异失去Focus下样式的问题

今天出现一个诡异的问题。解决的办法还挺出人意料的,记录一下。

一开始我发现,当sheet弹出后,TextField虽然可以正常输入,但是却没有文本输入的提示符,并且也没有选中的状态。

i9c9y-yxsu9

我尝试了很多办法。一开始我以为是第三方框架导致的。但是取消了第三方框架的引用之后,也还是有这个问题。后来我以为是sheet的模态导致的,但上后来我发现单独的主视图的TextField的也还是有这个问题。最后,我以为是Xcode的问题。但是新建的项目完全没有这个问题。

经过仔细思考,我突然想起来前两天在设计界面多主题切换的时候,我不小心删除过AccentColor,然后我就新建了一个AccentColor。目前的AccentColor是这个样子的。

new_accent_color

但是创建项目生成的AccentColor是这个样子的。

default_accent_color

解决

新建了一个项目,然后将Xcode默认生成的AccentColor拖动过来。问题解决。

0hgsw-9di0t

后续

我将这个问题作为bug报告给了苹果。反馈ID是FB15042261。

SwiftUI, Core Data and CloudKit Part 2

According to Apple, adding CloudKit Core Data, was as easy as following three steps.

  1. Enable iCloud Capacity in Xcode and check CloudKit.
  2. Add a default iCloud container.
  3. Replacing NSPersistentContainer with NSPersistentCloudKitContainer.

Then everything will work.

Using Core Data With CloudKit

It is not true. I did that and that won't work. I even checked the Core Data model with "Used with CloudKit", and there was still an error and CloudKit schema couldn't be created as there was no store for CloudKit.

So I had to create a new project with Core Data and CloudKit enabled at the very beginning.

If you want to know how to convert a Core Data project with CloudKit. See this, Getting Started with Core Data and CloudKit

Create a new project

Create a new project named "Todo List 2" and enable Core Data and CloudKit.

Project Code

  1. Add iCloud capability and check CloudKit.
  2. Then add a default container "iCloud.(You app bundle id)"

iCloud Capability

This is enough for a macOS app. If yours is an iOS app, you must add Background Modes capability and check "Remote notifications".

Be careful when you testing your app. The development and release app used different databases in CloudKit. So if you test your app, you must use the development version.

References