Post

Replies

Boosts

Views

Activity

Question about the Scope and "Inheritance" Behavior of SwiftUI Modifiers
I am confused about the "inheritance" behavior of modifiers in SwiftUI. Some modifiers (such as .background, .clipShape, etc.) seem to affect both parent and child views inconsistently. Here are some specific examples I encountered in Xcode 16.4 with the iOS 18.5 iPhone 16 Pro simulator: struct ContentView: View { var body: some View { VStack { // RedVStack Text("Hello world!") VStack { // OrangeVStack Text("Hello") Text("Hello") } .background(.orange) } .background(.red, in: RoundedRectangle(cornerRadius: 5)) // RedVStack has rounded corners, OrangeVStack also has rounded corners } } struct ContentView: View { var body: some View { VStack { // RedVStack Text("Hello world!") VStack { // OrangeVStack Text("Hello") Text("Hello") } .background(.orange) Text("Hello world!") } .background(.red, in: RoundedRectangle(cornerRadius: 5)) // RedVStack has rounded corners, OrangeVStack does not have rounded corners } } struct ContentView: View { var body: some View { VStack { // RedVStack Text("Hello world!") VStack { // OrangeVStack Text("Hello") Text("Hello") } .background(.orange) } .background(.red) .clipShape(RoundedRectangle(cornerRadius: 5)) // RedVStack has rounded corners, OrangeVStack does not have rounded corners } } I find it difficult to understand which modifiers affect child views and which do not. Is there any official documentation or authoritative explanation that can help me understand the scope and "inheritance" mechanism of SwiftUI modifiers? Thank you!
0
0
54
Aug ’25
Debugging Snapshot Thread Confinement Warning in UITableViewDiffableDataSource
I first applied a snapshot on the main thread like this: var snapshot = NSDiffableDataSourceSnapshot<Section, MessageViewModel>() snapshot.appendSections([.main]) snapshot.appendItems([], toSection: .main) dataSource.applySnapshotUsingReloadData(snapshot) After loading data, I applied the snapshot again using: Task { @MainActor in await dataSource.applySnapshotUsingReloadData(snapshot) } On an iPhone 13 mini, I received the following warning: Warning: applying updates in a non-thread confined manner is dangerous and can lead to deadlocks. Please always submit updates either always on the main queue or always off the main queue However, this warning did not appear when I ran the same code on an iPhone 16 Pro simulator. Can anyone explain it to me? Thank you
Topic: UI Frameworks SubTopic: UIKit Tags:
0
0
56
May ’25
Setting rounded corners via CAShapeLayer.path looks problematic
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white do { let shapeLayer = CAShapeLayer() shapeLayer.frame = CGRect(x: 50, y: 100, width: 200, height: 108) let path = UIBezierPath(roundedRect: shapeLayer.bounds, cornerRadius: 36) shapeLayer.path = path.cgPath shapeLayer.fillColor = UIColor.orange.cgColor view.layer.addSublayer(shapeLayer) } do { let layer = CALayer() layer.backgroundColor = UIColor.blue.cgColor layer.cornerRadius = 36 layer.frame = CGRect(x: 50, y: 300, width: 200, height: 108) view.layer.addSublayer(layer) } } } The corner radius is set to 36 through CAShapeLayer, but the actual effect is larger than 36, close to half of the height. Setting it through CALayer is fine Can anyone explain it to me? Thank you
Topic: UI Frameworks SubTopic: UIKit Tags:
1
0
267
Feb ’25
In WidgetKit, how to refresh automatically after the request fails?
I tried not calling completion in gettimeline. After about 40s, the system will call gettimeline again, but after 5 times, if completion is still not called, the widget refresh will be disordered. The set Timeline does not take effect, and the active call to WidgetCenter.shared.reloadAllTimelines() does not take effect. So now I didn't find a proper way to auto refresh. sample code: struct Provider: IntentTimelineProvider { static var count = 0 func placeholder(in context: Context) -> SimpleEntry { return SimpleEntry(date: Date(), name: "") } func getSnapshot(for configuration: OpenWidgetConfigIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { let entry = SimpleEntry(date: Date(), name: "") completion(entry) } func getTimeline(for configuration: OpenWidgetConfigIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { if Self.count < 4 { Self.count = Self.count + 1 return } else { Self.count = 0 } doTimeLineCompletion(name: "Sir Isaac Newton", image: nil, completion: completion) } func doTimeLineCompletion(name: String, image: UIImage?, completion: @escaping (Timeline<Entry>) -> ()) { var entries: [SimpleEntry] = [] var entry = SimpleEntry(date: Date(), name: name) entries.append(entry) let timeline = Timeline(entries: entries, policy: .after(Date().addingTimeInterval(TimeInterval(300)))) completion(timeline) } } struct SimpleEntry: TimelineEntry { let date: Date let name: String }
0
0
607
Jul ’22