Post

Replies

Boosts

Views

Activity

programmatic constraints and Interface Builder
I subclassed an UIView that is supposed to create it's owned constraints: @IBDesignable class MyCtrl: UIView { var label: UITextField! var _internalLayout = false public override init(frame: CGRect) { super.init(frame: frame) _init() } public required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) _init() } open override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() self._init() self.setNeedsDisplay() } func _init() { label = UITextField() addSubview(label) super.backgroundColor = .cyan #if TARGET_INTERFACE_BUILDER translatesAutoresizingMaskIntoConstraints = true #else translatesAutoresizingMaskIntoConstraints = false #endif } override public func layoutSubviews() { if _internalLayout { _internalLayout = false return } let theOrientation = orientation setupConstraints(theOrientation) super.layoutSubviews() } func setupConstraints(_ orientation: IosTraitClass) { _internalLayout = true superview?.removeConstraints(superview!.constraints) if orientation == .CR { label.text = "setupConstraints(.CR \(superview?.frame.size.width)" label.sizeToFit() superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: superview , attribute: .leading , multiplier: 1, constant: 30)) superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: superview, attribute: .top, multiplier: 1, constant: 100)) superview?.addConstraint( NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: .none, attribute: .notAnAttribute, multiplier: 1, constant: (superview?.frame.size.width ?? 0)-60)) superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: .none, attribute: .notAnAttribute, multiplier: 1, constant: (superview?.frame.size.height ?? 0)-300)) } else if orientation == .RC || orientation == .CC { label.text = "setupConstraints(.CC ou .RC \(superview?.frame.size.width)" label.sizeToFit() superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: superview, attribute: .trailing, multiplier: 1, constant: -50)) superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: superview, attribute: .top, multiplier: 1, constant: 50)) superview?.addConstraint( NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: .none, attribute: .notAnAttribute, multiplier: 1, constant: (superview?.frame.size.width ?? 0)-100)) superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: .none, attribute: .notAnAttribute, multiplier: 1, constant: (superview?.frame.size.height ?? 0)-100)) } } } In the method layoutSubviews of my component, I called orientation which is an extension of UIView: public var orientation: IosTraitClass { if UIScreen.main.traitCollection.horizontalSizeClass == .regular && UIScreen.main.traitCollection.verticalSizeClass == .compact { return .RC } else if UIScreen.main.traitCollection.horizontalSizeClass == .compact && UIScreen.main.traitCollection.verticalSizeClass == .regular { return .CR } else if UIScreen.main.traitCollection.horizontalSizeClass == .compact && UIScreen.main.traitCollection.verticalSizeClass == .compact { return .CC } else if UIScreen.main.traitCollection.horizontalSizeClass == .regular && UIScreen.main.traitCollection.verticalSizeClass == .regular { return .RR } else { return .none } } Finally I made a UIViewController: class ViewController: UIViewController { @IBOutlet weak var container: MyCtrl! override func viewDidLoad() { super.viewDidLoad() setupConstraints() } func setupConstraints() { container.setupConstraints(view.orientation) } func calculatedConstraints() -> [NSLayoutConstraint] { return []; var constraints = [NSLayoutConstraint]() container.removeConstraints(container.constraints) if view.orientation == .CR { //constraints.append(contentsOf: container.setSize(width: (view?.frame.size.width ?? 0)-60, height: (view?.frame.size.height ?? 0)-300)) } else { //constraints.append(contentsOf: container.setSize(width: (view?.frame.size.width ?? 0)-40, height: (view?.frame.size.height ?? 0)-100)) } return constraints } override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { container.setupConstraints(view.orientation) super.viewWillTransition(to: size, with: coordinator) } } When I run my program the constraints are well applied in landscape or portrait mode. But when I look in the interface Builder, constraints are not applied, and I don't understand Why
2
0
1.1k
Aug ’21
Howto remove coredata from ios app
I have an IOS application who uses coredata As I want to develop and App extension which needs to share coredata with my app(Yes, I made a group App), I thought a good solution would be to create a framework with the, coredata model. I did it, but after that the coredata of the main app is no longer necessary. and I think I made a mistake when removing the file xcdatamodel from my app. when I refer to an entity in the main application, it doesn't use the entity of the framework. when I right click on an entity instance in my main application, I see a file which is: entity+CoreDataClass and of course I don't find it in the finder. Did I made a mistake by simply remove the xcdatamodel from my App? Do I have something to clean?
0
0
857
Jul ’22
async await invalid conversion from throwing function
I'm complety lost with async await operation I have a subclass of UITextField which provide a verif method in the resignFirstResponder method, I call the verif Method and return true or false, depending of the result of the verif the verif method is asynchrone, because it can make call to a web server. My code is: import UIKit public enum VerifResult<S: NSString> { case success case failure(S) } @IBDesignable class TextField: UITextField { @IBInspectable var obligatoire: Bool = false override func resignFirstResponder() -> Bool { var bRes = false do { let res = try await withCheckedThrowingContinuation { continuation in let result = try await verif() continuation.resume(returning: result) } switch res { case .success: bRes = true case .failure(let msg): bRes = false } } catch { return false } return bRes } public func verif() async throws -> VerifResult<NSString> { if obligatoire && text == "" { return .failure("Zone obligatoire") } else { // make call to a webserver and return .sucess or .failure return .success } } } in the resignFirstResponder on the line try await, the compiler detects the error: Invalid conversion from throwing function of type '(CheckedContinuation<_, Error>) async throws -> Void' to non-throwing function type '(CheckedContinuation<T, Error>) -> Void' I'm lost, I don't understand why where is my mistake?
6
0
2.7k
Aug ’22
Using framework which use alamofire
I have a framework named NetworkFramework which uses alamofire. Of course, somewhere in my sources, I have : import Alamofire My framework has a podfile which contains: target 'NetworkFramework' do use_frameworks! # Pods for NetworkFramework pod 'Alamofire', '~> 5.5.0' end if I want to use this framework in an App I have 2 possibilities (if I don't mistake) import NetworkFramework and his sources in the app simply import NetworkFramework.framework In the first case, all is compiled and there is no problem. In the second case, when I try to compile the App, I got an error which is No such module Alamofire I don't understand why. Do I have to compile the App with the sources of the framework?
0
0
1k
Nov ’22
Ios downloaded locations
I develop an App which has a quicklook preview for files located on a web server To realize this preview, I download the file and access it with a QLPreviewController When I use simulator, I can see the downloaded files somewhere in library/developper/coresimulator..... and so on of my mac, and I can manually delete them. But if I search these files on the IPhone simulator, I can't find them. What when I run the application on my real device? How will I be able to delete these files? because I think it take place in the IPhone
4
0
1.3k
Dec ’22
showing QLPreviewController in a containerView
I see there is not many answers about quicklook posts I have an IOS app which display items in a QLPreviewController and it works well when I presenty the QLPreviexwController. Now I want to change the presentation: I want a ViewController with 2 viewContainer: One for informations about the object represented by quicklook, and the other with the previewController itself. When I run the nw App, I don’t see the preview in the second viewContainer. The function numberOfPreviewItems is called, but previewItemAt is never called. Do you think it is not possible to show a quicklook item in a viewContainer?
0
0
1.2k
Dec ’22
Compiler problem
I develop a share extension for my app the app compile and runs with no problem, but when I add a share extension to my App, without changing anything to the code of apple, I have error: Unexpected input file: /Users/patricerapaport/Swift/Rangements/RangementsIOS/DEINIT_WARKING and unable to open dependencies file (/Users/patricerapaport/Swift/Rangements/RangementsIOS/Build/Intermediates.noindex/RangementsIOS.build/Debug-iphonesimulator/RangementsShareExt.build/Objects-normal/x86_64/RangementsShareExt-master-emit-module.d) I precise that my App and my extension share the same group app My version of Xcode is 14.1
2
0
989
Dec ’22
custom keyboard and rightToLeft direction
I'm developping a hebrew custom keyboard (the way the wovels are handled in the apple hebrew keyboard is not satisfying for me). As the hebrew language is from right to left, I expect that when I type a letter the cursor position moves to the left of the text, but it is not the case: it stays on the right, while new typed caharcters will be correctly positionned (on the left of the last character). if I manually position the cursor on the left of the last character entered (using adjustTextPosition of textDocumentProxy) the next character whill not be on the left position, as expected but on the right position. how can I fix the problem, and have the cursor on the left after typing a character?
0
0
810
Feb ’23
Howto releaseData with UITableView prefetching
I develop an app with a UITableView extracting data from a web server. to spare communication between my app and my webserver, I calculate how many rows my tableView can display and I ask the web werver to send me a array containing this number of data. when prefetching,rows, if the ;data corresponding to the indexPath is not in memory, I call the web server, otherwise I extract the data from memory. everything works fine, but I notice that the amount of data grows at each call to the webserver, and is never released. to release some data, I thought using tableview(cancelPrefetchingForRowsAt), but i'm not sure it is the good way to do it. I'm not sure that indexPathes calld in the cancelPrefetching method correspond to rows that UITableView doesn't use
0
0
355
Feb ’23
SwiftUI Modifiers on generic Views
I created a control which group a TextField, a Text as the label of the TextField, and eventually a Text to signal an error The result will be something like that: To declare this control, I write the following Code: struct TextFieldWithError<T: Hashable>: View { var label: String @Binding var text: String @Binding var textError: String var focusId: T? @FocusState.Binding var focus: T? var body: some View { VStack { HStack { if layout == .hstack { Text(label) } if let focusId { TextField(label, text: $text) .focused($focus, equals: focusId) } else { TextField(label, text: $text) .modifier(TextFieldStyle()) } } if textError != "" { Spacer() .frame(height:2) HStack { Text(textError) .foregroundColor(.red) .frame(height: textError == "" ? 0 : 20) .animation(.default) } } } } } if I use a generic for my control, it's because I want to control focus with the .focused modifier and I didn't found another way to do this now, I want to control the width of the label with a modifier. I thought about writing .TextFieldWithErrorFrame(labelWidth: <some CGFloatvalue>) struct TextFieldWithErrorFrame: ViewModifier { varlabelWidth: CGFloat func body(content: Content) -> some View { content } } I think that in place of writing content in the body of my modifier, I have to write the same code that I wrote for the control body, applying a frame modifier on the Label. But I don't find the way of doing this. if I declare the modifier as this: struct TextFieldWithErrorFrame<T>: ViewModifier where T: Hashable { var width: CGFloat func body(content: TextFieldWithError<T>) -> some View { content } } I need to declare an type alias for the body, because I have the error: Type 'TextFieldWithErrorFrame' does not conform to protocol 'ViewModifier' if I use typealias Body = TextFieldWithErrorFrame<T> I have the same error and that's not all. how to rewrite the content in the body of the modifier?
0
0
870
Jun ’23
Swiftui and verticalTraitClass
For my app working on iPhone and IPad, I have two different designs depending on the landscape or portrait orientation. But it's not so simple, because for iPad, the design is the same while in portrait or landscape orientation. So I thought to user verticalSizeClass. If it is compact, I apply portrait design, if it is Regular, I apply landscape design. So I introduce the environment variable: @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass? And create the following modifier (found on Apple Developper Forum) struct DeviceRotationViewModifier: ViewModifier { let action: (UIDeviceOrientation) -> Void func body(content: Content) -> some View { content .onAppear() .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in action(UIDevice.current.orientation) } } } // A View wrapper to make the modifier easier to use extension View { func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View { self.modifier(DeviceRotationViewModifier(action: action)) } } Implementing the OnRotate modifier on my contentView .onRotate { newOrientation in print(verticalSizeClass) } I find that the verticalSizeClass is not correct on first call of OnRotate (it's always regular). After when I change orientation, the verticalsizeClass becomes correct. What did I missed?
0
0
508
Sep ’23
ipad constraint and orientations
I have an App for Iphone and Ipad. in my UIViewController, the subviews installed depend of the different classes (CR, RC, CC, RR) because possible widths of the view are not the same. This work very well on Iphone, because the width of the view is different, depending on the class (CR, RC, CC). But for Ipad, the class is RR, no matter the orientation. Or in portrait and landscape, of course, width is not the same. In Interface Builder, I don't find a way to have subviews installed for Ipad in landscape and not in Portrait. Is there a way to handle this, or do I have to handle this programatically?
3
0
1.7k
Jun ’22
Interface Builder and subclassed UIView
I test a very simple thing: I have a Subclassed UIView as follow: @IBDesignable class myView: UIView {          override public init(frame: CGRect) {         super.init(frame: frame)     }     required public init?(coder aDecoder: NSCoder) {         super.init(coder: aDecoder)     }       override open func layoutSubviews() {         super.layoutSubviews()         layer.frame.size.height = 50         layer.borderWidth = 1         layer.borderColor = UIColor.green.cgColor     } } To install this component in Interface Builder, I add an UIView and I change the class name in the Inspector. When I add the UIView, the height of this view is 128. I suppose it is a default value of IB. But I want this height to be 50. When I change the name of the class in the IB inspector, the view is correctly drawn, white a height of 50, but when I select it, the size of the selection is the size of a default UIView Is there a mean to correct this?
3
0
789
Sep ’21
swift and AppDirectory
I develop an App in macos, and I need to access the documentDirectory: let dialogURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first I get: file:///Users//Library/Containers/<devTeam.appName>/Data/Documents But in the Library/Containers directory I don't have the <devTeam.appDirectory> (in my case "PR.medical") but (in my case "medical") of course, it would be possible to hard code the name of the desired directory, but if in the future apple change anything, my app will not work properly. Am I missing something?
4
0
1.1k
Nov ’21
getting the real widt of an UIView in ios
For some programming reason, I need to know the real size (width and height) of an UIView wich has constraints. Depending on the device I use, and depending on the orientation of the device, the width or height of my view change, but frame and layer frame don't give me the correct result. Is there a mean to have the size?
6
0
1.1k
Jul ’22