Post

Replies

Boosts

Views

Activity

How to update UIConfigurationState from CollectionView using UIConfigurationStateCustomKey
is there anybody using UIConfigurationStateCustomKey to manage custom state for cells? I want to know if that would be possible to update from the collection view, it’s like UICollectionView.select(indexPath) seems like there is no way for this though. currently UICollectionView (UITableView) can manage is-selected for cells. but provided official property are only available. if we need something another custom properties that describes the cell’s state, we might need make state manager for cells. I was wondering how UIConfigurationStateCustomKey works for us for such case. According to the documentation, we can update the custom state from only visible cell. so we can not update currently hidden cell’s state. https://developer.apple.com/documentation/uikit/uiconfigurationstatecustomkey
0
0
324
Dec ’23
iOS 17 @Obervable object - how to make it lazy to init once for the identical view?
iOS 17 introduced @Observable. that's an effective way to implement a stateful model object. however, we will not be able to use @StateObject as the model object does not have ObservableObject protocol. An advantage of using StateObject is able to make the object initializing once only for the view. it will keep going on until the view identifier is changed. I put some examples. We have a Observable implemented object. @Observable final class Controller { ... } then using like this struct MyView: View { let controller: Controller init(value: Value) { self.controller = .init(value: value) } init(controller: Controller) { self.controller = controller } } this case causes a problem that the view body uses the passed controller anyway. even passed different controller, uses it. and what if initializing a controller takes some costs, that decrease performance. how do we manage this kind of use case? anyway I made a utility view that provides an observable object lazily. public struct ObjectProvider<Object, Content: View>: View { @State private var object: Object? private let _objectInitializer: () -> Object private let _content: (Object) -> Content public init(object: @autoclosure @escaping () -> Object, @ViewBuilder content: @escaping (Object) -> Content) { self._objectInitializer = object self._content = content } public var body: some View { Group { if let object = object { _content(object) } } .onAppear { object = _objectInitializer() } } } ObjectProvider(object: Controller() { controller in MyView(controller: controller) } for now it's working correctly.
1
0
673
Oct ’23
How to take values from custom multiple attributes in AttributedString - like ^[Text](arg1: '', arg2: '')
According to the WWDC 21 Session https://developer.apple.com/videos/play/wwdc2021/10109/ It shows how can we describe custom attributes in markdown as followings: This text contains ^[an attribute](rainbow: 'extreme'). This text contains ^[two attributes](rainbow: 'extreme', otherValue: 42). This text contains ^[an attribute with 2 properties](someStuff: {key: true, key2: false}). So, the first one, I can guess how to create, and last one could archive with describing Value type as a Struct. However, I don't know how to handle the second case. CodableAttributedStringKey and MarkdownDecodableAttributedStringKey allow us to override decoding function. but that decoding function's Decoder can only accept to access to the value which comes from first attribute. Like rainbow's container. So how can we take the value in otherValue as 42?
2
0
757
Jun ’22
Using UINavigationBar standalone causes weird top attaching layout
Against using UINavigationController + UINavigationBar. This question is about what if we use UINavigationBar as standalone in a ViewController. To lay UINavigationBar out as well as it is the case of UINavigationController management, I'm doing like this, Add UINavigationBar to the view of ViewController Add constraints to lay it out on top of the safe area. navigationBar.bottom == safeAreaLayoutGuide.top navigationBar.left == view.left navigationBar.right == view.right Set the delegate of UINavigationBar, then return topAttached for the bar position. Set additionalSafeAreaInsets to propagate the insets to the subviews. It looks works well but got weird laying out inside UINavigationBar from seeing view debugger. The extending view to cover to the top of the screen by setting `topAttached is too much growing. What am I missing? I'd like to know how to use UINavigationBar in the correct way that includes safe-area management.
2
0
942
Feb ’22
How to prevent Xcode builds Swift packages for all arches except using Debug configuration?
In Debug configuration, Xcode builds the packages for the currently active arch only. But if we create extra configuration by cloning Debug, those schemes make Xcode builds the packages for all architectures. It seems like Xcode misleading that the scheme is for release. How to specify the option to build for active arch only, like Xcode build settings ONLY_ACTIVE_ARCH?
0
0
585
Jan ’22
[For simulator] How to build Swift packages as x86_64 in Xcode and M1 mac (Apple silicon) without using rosetta
On M1 mac (Apple Silicon chips), Xcode runs on arm64. How can we build the packages as x86_64 in SwiftPM that integrates with Xcode? Xcode builds the target as arm64 for simulator unless specifying excluded archs option. In some cases, the project would be forced to use x86_64 for simulator build since using external prebuilt frameworks only have arm64 for the device and x86_64 for the simulator. with specifying EXCLUDED_ARCHS[sdk=iphonesimulator*] = arm64 However, I could not find a way to specify that in the packages managed by SwiftPM. From this, Xcode builds packages as arm64 then builds application target as x86_64, linking phase would fail with not found modules in the arch. What can we do when we are forced to use still x86_64 on M1 mac?
7
1
5.6k
Jan ’22
[iOS15.0.0] UIRefreshControl jumps content-offset when it starts refreshing in UIScrollView
From iOS 15.0, UIRefreshControl works weird when it starts refreshing. This component provides refreshing content interaction by pulling down in scrollable view (e.g. UICollectionView, UITableView, UIScrollView) However, at the time to start refreshing, the content offset of the scroll view jumps a bit. It never happens prior to iOS15. I created a project that reproduces. https://github.com/muukii/iOS15-UIRefreshControl-issue-reproduction
0
0
1.2k
Oct ’21
[Xcode 12 beta.1] @State does not project a value correctly that set from an event handling closure.
While I'm trying to use SwiftUI on Xcode12 beta.1. I found @State does not work correctly under the specific case. That issue will appear in following: @State var selectedValue get a new value from onTapGesture of something view. And then, attempt present sheet. That sheet presentation needs the view that contains selectedValue selectedValue must have the set value, because it will run afeter modified selectedValue. but it does not. struct ContentView: View {  @State var selectedValue: Int?  @State var isPresented = false  var body: some View {   VStack {    Text("Tap this number")     .onTapGesture {      selectedValue = 1      isPresented = true     }   }   .sheet(isPresented: $isPresented) {    Text(selectedValue!.description)   }  } } But if we do this, the issue solves. struct ContentView: View {  @State var selectedValue: Int?  @State var isPresented = false  var body: some View {   VStack { &#9; Text(selectedValue.debugDescription)    Text("Tap this number")     .onTapGesture {      selectedValue = 1      isPresented = true     }   }   .sheet(isPresented: $isPresented) {    Text(selectedValue!.description)   }  } } I just added a line that reads the value of selectedValue
4
0
1.3k
Jul ’20