Post

Replies

Boosts

Views

Activity

Resize image with autolayout
My app loads an image at runtime and I want to display it inside a table cell. To do that I created a UIView (trailing/leading/top/bottom: 10) inside the cell and added a UIImageView as its child. What constraints do I need for the child, so the image keeps its proper aspect ration, isn't wider than the width of the screen and there's no unused space (apart from the constraints)? I tried to use: Trailing/leading/top/bottom: 0 Align center x/y This works if the image uses the portrait format but with landscape there's unused space above and below the image. I also tried to set the view's height to ">=50" but no success and I can't just set a max. value because my app has to work on both iPhones and iPads.
2
0
1.3k
Oct ’21
"Unexpectedly found nil while unwrapping an Optional value" with UIColor
Xcode 13.0 is driving me crazy at the moment... I've got a couple of color sets (default sRGB with "Any Appearance" & "Dark") that are used in various UIViewControllers. Two of them I also use in a custom button class: class SpecialBorderButton:UIButton { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { if #available(iOS 13.0, *) { super.traitCollectionDidChange(previousTraitCollection) if (traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection)) { layer.borderColor = UIColor(named: "ButtonBorderColor")!.cgColor } } } } What the code does: When the theme is changed while the app is running, usually the border of a button wouldn't change until you reload the ViewController. traitCollectionDidChange is called when the theme changes and this way the border color is set properly too. The issue navigator has been throwing the same error for 3 different UIViewControllers since I installed Xcode 13: Failed to render and update auto layout status for ViewController (.....): The agent crashed The crash report in Library/Logs/DiagnosticReports says: CoreSimulator 776.3 - Device: iPhone SE (1st generation) (...simulator code...) - Runtime: iOS 15.0 (19A339) - DeviceType: iPhone SE (1st generation) myApp/SpecialButton.swift:26: Fatal error: Unexpectedly found nil while unwrapping an Optional value First of all, I don't even use the SE 1G/iOS 15 simulator but always the SE2/iOS13 one, also, I checked one of the buttons that use that class and pressed "Debug" next to "Designables Crashed" in the Attribute Inspector and it blames the same line: layer.borderColor = UIColor(named: "ButtonBorderColor")!.cgColor ... with the same error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value The reason why I'm writing this question: The color exists! I checked the spelling multiple times and also copied the name but the errors are still there. I deleted derived data, cleaned the build folder and restarted Xcode and the mac multiple times but the error never fully vanish for all 3 ViewControllers. There aren't any wonky connections to that button either (as far as I've seen). I use UIColor(named:....) with different colors in multiple other classes too (but without the custom class) and they're fine there, it's just this class that's affected, for whatever reason. How do I fix this?
1
0
2.8k
Sep ’21
Temporarily switch from background thread to main thread (and back?)
I've got a function that runs on a background thread and downloads files: func downloadFiles(remoteFolder rf:String, localFolder lf:String, completion: @escaping (_ success:Bool, _ err: String) -> Void) { DispatchQueue.global(qos:.background).async { ... DispatchQueue.main.async { completion(successBool, errorMsg) } } } It's called by my main ViewController: myDownloadClass.downloadFiles(remoteFolder: rf, localFolder: lf, completion: { (success, error) in ... //More stuff here } Is there a way to temporarily switch back to the main thread (and with that also to the VC) in downloadFiles, so I can add the name of the file that's next in the download list to an e.g. UIAlertController but without tripping off everything that should be done after downloading everything actually finished ("More stuff here")? I'm using Xcode 13 with Swift 5 and iOS 13.
2
0
2.8k
Sep ’21
Xcode crashes when I set an aspect ratio in XIB
Whenever I try to set an aspect ration between two elements (e.g. parent UIView with child UIImageView) by pressing ctrl and dragging from one to the other, then picking "Aspect Ratio" in the new popup list, Xcode crashes. Sometimes I can see the new constraint for a second in the inspector before the crash happens and it says: (null) Ratio to: Img View Already sent 2 bug reports, no point sending any more than that. I'm using Xcode 12.5, I know a new version is out but it won't let me install it because apparently there isn't enough space left (there are 18gb!)... This now also bricked my main storyboard and I can't open it with the IB anymore: The document "Main.storyboard" could not be opened. The operation couldn’t be completed. (com.apple.InterfaceBuilder error -1.)
3
0
901
Sep ’21
Find all active "print()"s
Is there a way to find all active print()s in an Xcode 12.5 project, so all print()s that aren't preceded by a // or //[space]? I know that you can search the whole project with the "Find Navigator" (left side) but searching for print(" finds all the active and the inactive ones and //print(" only lists the inactive ones, of course. Unfortunately the search options (Containing, Matching Word, Starting With, Ending With) don't help here (unless I missed something).
1
0
536
Jun ’21
Set button border according to light/dark mode
Xcode 12.5/iOS 13+/Swift 5 I've got multiple UIButtons and I change their border color in their "Identity Inspector" with borderColor set to the custom ColorSet "ButtonBorderColor". The button's text and background change properly when I switch to dark mode but the border doesn't. According to Stackexchange you have to override traitCollectionDidChange like this: override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { if (traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection)) { layer.borderColor = UIColor(named: "ButtonBorderColor")!.cgColor } } But where do you put this, so it affects every button in my app? I tried to add it to an extension UIButton {}, which told me to add open to the function but the color doesn't change when I switch mode.
1
0
3.1k
Jun ’21
How do I use "Base" language & "Development Language"?
In the "Info" tab of my project (Xcode 12.5, Swift 5) there are two languages in the "Localizations" list: Base (2 Files Localized) English - Development Language (0 Files Localized) I can add new languages but it won't let me remove either of the above. First of all, what are "base" and "development language" used for? Am I right in my assumption that the former is the language that's used by default if the user's phone is set to any language that isn't English and the latter is the language that is always used in Xcode's simulators (and if the user's phone is set to English)? In the File Inspector for "Main.storyboard" and "LaunchScreen.storyboard" "Base" is ticked and "English" isn't. What's the point of that? Do these specific lists show what languages that specific storyboard supports? Let's say I want to use e.g. French both as base and development language (according to my assumptions) and English if the user's phone isn't set to French. How do I accomplish that?
5
0
4k
May ’21
Is there a problem with this solution for an expandable UITableCell?
Hey everyone! I've got a UITableView and want to fill it with cells that expand to either show a UIlabel with some additional info or a UIImage when tapped. Googling shows mostly solutions for phone book like apps and they all use sections (one for each letter) and rows within that section (for the actual person entry). How I solved it: Create multiple prototype cells with one subclass of UITableViewCell for each, set some parameter in didSelectRowAt & reload the table load there too, then just pick the right prototype cell according to that parameter in cellForRowAt. This seems like a pretty easy and straight forward way of doing this and yet I haven't come across anyone else doing it that way. Are there any performance problems doing it like this and that's why nobody is? What's the recommended way of doing this when the expanded part of your UITableViewCell doesn't look the same for every cell?
3
0
838
Mar ’21
Custom popup: UIView or UIViewController?
My apps adds buttons at runtime and each of them has to open a popup. Unfortunately Apple doesn't let you add a custom view to a UIAlertController (instead of the message) and people highly advice against trying to hack it it because it might break in future iOS versions, which means that I'm going to have to create my own custom view. The new view has to act exactly like a popup, so: Displayed on top of the calling UIViewController Don't allow clicking through Close/remove the view again by clicking the "close" button My question: What should I use for that? A UIView in an extra .xib file (instantiate at runtime) or a new UIViewController in my existing .storyboard file (create some type of "show" segue at runtime)?
4
0
2.4k
Mar ’21
Storyboard: How do I change the iOS version to see layout changes?
My app is made for iOS 12.4, so I usually use the iPhone SE 12.4 simulator (Xcode 12.4). Switching to the iPhone SE 2 simulator (iOS 14.4) temporarily I noticed that the date/time pickers now look completely different: They used to be simple scroll wheels but now you have to tap on the element to open the actual picker. The date picker now also opens an actual calendar, which I'm not a fan of because it's tedious to change the year that way. How do I make the storyboard reflect these changes, so I can make everything fit again? I remember that there was a way to simply set the iOS version for it (without changing the deployment info for the whole project) but now that I'm looking for it I can't find it of course.
2
0
1.2k
Mar ’21