简体   繁体   中英

Sharing UI elements between main App and Today widget/extension in Swift

I have a few UI elements on my app's ViewController (mainly UILabels). I am having to recreate these on my Today extension's View Controller. This does not seem right. Is there a way to share/reuse these UI elements; for example in a container view?

There are a few ways to do this. (Swift modules, frameworks, static libraries)

The simplest way to get what you want is to just add the view's file to the Build Sources of the Today widgets target.

If you're UI component is AwesomeView.swift it'll be listed under:
Xcode Project Settings > Targets > MainApp > Build Phases > Compiled Sources

Go to:
Targets > TodayWidget > Build Phases > Compiled Sources

Then click the + and add the AwesomeView.swift file to the today widget target as well. Now it will be compiled into, and can be used by, both binaries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM