13 votes
Accepted

Xcode 15 - Preview macro error with UIKit UIView

I find that restricting this one-statement preview to iOS 17 works: @available(iOS 17, *) #Preview { return UIView() } However, this two-statement preview still does not work: @available(iOS 17, *...
rob mayoff's user avatar
  • 381k
11 votes

Cannot place SwiftUI view outside the SafeArea when embedded in UIHostingController

iOS 16.4 Update There's now an official API in iOS 16.4 for the previously private API method: viewController.safeAreaRegions = SafeAreaRegions() The SafeAreaRegions() initialiser returns an empty ...
Pranav Kasetti's user avatar
10 votes
Accepted

‘traitCollectionDidChange’ was deprecated in iOS 17.0. How do I use the replacement?

traitCollectionDidChange is deprecated as of iOS 17.0. The solution depends on your app's deployment target. If your app's deployment target is older than iOS 17.0 (Xcode 15 supports back to iOS 12) ...
HangarRash's user avatar
  • 13.3k
8 votes
Accepted

Xcode 15 UIKit Preview FailedToLaunchAppError: Failed to launch error

Our project could not initially be debugged using the simulator in Apple silicon, so it could not be previewed. After the arm64 architecture is excluded from each target: the simulator works, but #...
XavierZzz's user avatar
  • 122
7 votes

Custom smaller Detents in UISheetPresentationController?

In UIKit - iOS 16+ fraction detent can be implemented like this: viewControllerToShow.modalPresentationStyle = .formSheet let sheet = viewControllerToShow.sheetPresentationController let ...
Andrey Volobuev's user avatar
7 votes

Tabbar middle button utility function in SwiftUI

just do a ZStack with a Button or any view you want... struct MainTabView: View { @State private var selection = 0 var body: some View { ZStack(alignment: .bottom) { ...
Peter Lapisu's user avatar
  • 20.5k
5 votes

How to add placeholder text to TextEditor in SwiftUI?

Native Solution from iOS 16 Instead of TextEditor use TextField. Since iOS 16 it supports multiple lines and of course place holder natively, when using one of the initializers with the axis parameter....
heyfrank's user avatar
  • 5,479
5 votes

`SwiftUI` - `EnvironmentObject`s, `EnvironmentValues` does not propagate to modally presented `UIViewController`

A full environment (values plus objects) can be passed to a manually created UIHostingController to be presented. It is not obvious, but this technique works: 1. Obtain the whole environment Option a) ...
Pavel Holec's user avatar
5 votes

Is there an easy way to pinch to zoom and drag any View in SwiftUI?

Based on your solution, I've implemented more dynamic struct that allows you to use it anywhere you'd like with any kind of View you'd like, animates double tap scale changes and zooming to the ...
Ido's user avatar
  • 587
4 votes

How to open "Preferred Language" Page in iOS Settings App programmatically?

There is no way to open inside of Preferred Languages. You can open the app settings page as your screenshot image. openSettingsURLString is the url for it. if let url: URL = .init(string; ...
Yusuf's user avatar
  • 901
4 votes

Preview on multiple devices option in Xcode

Right now, my current Xcode version is 14.3.1. For those who want to see the Preview in the Storyboard Builder: Click on the Adjust Editor Options Click on Assistant You can also find the Assistant ...
Carlos Leonardo Camilo Vargas's user avatar
4 votes
Accepted

Can't assign instance of a Swift class conforming to a protocol to an Objective-C property expecting that protocol

Some observations: The documentation page for UIContentConfiguration doesn't have an Objective-C version that you can choose using the "Language: Swift" selector. The Objective-C version is ...
Sweeper's user avatar
  • 238k
3 votes

How to create the equivalent of a SwiftUI stack view spacer programmatically

import UIKit extension UIView { public static var spacerView: UIView { let view = UIView() view.isUserInteractionEnabled = false view.setContentHuggingPriority(....
liudasbar's user avatar
  • 234
3 votes

Dismiss a SwiftUI View that is contained in a UIHostingController

iOS 15 and above struct MyView: View { @Environment(\.dismiss) var dismiss var body: some View { NavigationView { Text("Hello World") .toolbar { ...
Jevon718's user avatar
  • 424
3 votes

How to change the corner radius of UISegmentedControl?

Rounded corners of the view and its selectedSegment Create a custom subclass of UISegmentedControl: final class SegmentedControl: UISegmentedControl { private var cornerRadius: CGFloat init(...
Viktoryia's user avatar
3 votes
Accepted

Should I use viewDidLayoutSubviews() or traitCollectionDidChange() to respond to changes in interface orientation

Option C: viewWillTransition(to:with:) might be the better choice to update constraints. You can make a decision based on the new size and you can animate the changes by making use of the ...
HangarRash's user avatar
  • 13.3k
3 votes

Why not create an instance of a UIView and straight away assign to a property instead of using implicit unwrapped optionals?

I don't think the implicitly unwrapped optional is that popular, except when used in conjunction with Storyboards, where it would look like: @IBOutlet var label: UILabel! The option which I see more ...
timbre timbre's user avatar
3 votes
Accepted

Vertical Paging UIScrollView incorrect height

Just add .ignoresSafeArea() in the VStack struct FeedView: View { var body: some View { VStack(spacing: 0) { Text("") .frame(width: UIScreen.main.bounds.width, height:...
Supriyo Dey's user avatar
3 votes
Accepted

How to make iOS app that support different languages

you have to create the localisation file steps are : Go to project Navigations Click on the project In the Info tab scroll down to see the Localisation section, in that section you can select the ...
Rajni Bajaj's user avatar
3 votes
Accepted

How to have UIButton image using UIButton.Configuration adapt and resize dynamically to the button size

You are absolutely right that the image is not resized to fit the button; it's the other way around. Let the image, plus the configuration's contentInsets, size the button for you. Here's an example (...
matt's user avatar
  • 524k
3 votes
Accepted

Content of UIViewRepresentable in a ScrollView is not rendered properly

I faced the same problem, from iOS 16 and up, UIViewRepresentable has a new method sizeThatFits(_:uiView:context:) -> CGSize? that helps with this. I implemented it as follows: func sizeThatFits(_ ...
Patrick's user avatar
  • 458
3 votes
Accepted

Tap Gesture not working on the Circle Swift

Views - buttons, labels, image views, plain views, etc - do not receive touches when the .alpha is less than 0.01 What may not be obvious is that when we write an animation block like this: UIView....
DonMag's user avatar
  • 73.5k
3 votes
Accepted

Is it possible to change a custom trait from a parent trait collection and observe the changes to all view controllers?

The UIKit trait system is a mechanism to propagate data through the tree structure of your app's hierarchy. You can see illustrations of the trait hierarchy in the WWDC video you mentioned — but the ...
smileyborg's user avatar
  • 30.4k
3 votes
Accepted

iOS 17 UIKit Popover Crash on iPad - UIPopoverPresentationController should have a non-nil sourceView

I get the same crash on iPadOS 17 too. It doesn't happen all the time so it's tricky why this happens. My current workaround is to guard check if the current view controller is presenting another view ...
Joe Pham's user avatar
3 votes
Accepted

How to turn off device rotation programmatically - iOS

I'm assuming your default rotation is .portrait. So in AppDelegate file, conform supportedInterfaceOrientationsFor then declare a property UIInterfaceOrientationMask, set it to .portrait. @main class ...
sonle's user avatar
  • 5,834
3 votes
Accepted

Animate UIBarButtonItem with iOS 17 pulse symbol effect not working (without UIImageView)

SOLVED I don't think this is at all obvious, but if you save a handle to the bar button item as an instance variable and then start the animation only after the view appears, the effect is visible. ...
Mario's user avatar
  • 2,497
3 votes
Accepted

NSViewRepresentable.updateNSView is not called unless the modified binding variable gets used inside

My guess is that SwiftUI "smartly" detects which binding variables are used inside updateNSView(), and only calls it when there is a change to one of those variables This is basically ...
Sweeper's user avatar
  • 238k
2 votes

UISystemGateGestureRecognizer and delayed taps near bottom of screen

As of iOS11, the correct way to override these gestures is to override preferredScreenEdgesDeferringSystemGestures in your view controller. open override var ...
olynoise's user avatar
  • 2,046
2 votes

How to change the colors of a segment in a UISegmentedControl in iOS 13?

SegmentedControl background color Create a custom subclass of UISegmentedControl (if you don't want to see the divider imageView between the segments): final class SegmentedControl: ...
Viktoryia's user avatar
2 votes

How can I give WKWebView a colored background

What worked for me in macOS 14 let webview = WKWebView() webview.setValue(false, forKey: "drawsBackground")
cherucole's user avatar
  • 628

Only top scored, non community-wiki answers of a minimum length are eligible