I am encountering the following strange behaviour in SwiftUI. Assume these definitions:
import SwiftUI
let cardAspectRatio : CGSize? = CGSize(width: 63, height: 88)
extension View {
@ViewBuilder
func fitCardAspectRatio1() -> some View {
let ratio = cardAspectRatio!
self.aspectRatio(ratio, contentMode: .fit)
}
@ViewBuilder
func fitCardAspectRatio2() -> some View {
if let ratio = cardAspectRatio {
self.aspectRatio(ratio, contentMode: .fit)
} else {
fatalError()
}
}
func fitCardAspectRatio3() -> some View {
let ratio = cardAspectRatio!
return AnyView(self.aspectRatio(ratio, contentMode: .fit))
}
}
Should not all three methods behave equivalently? I know that internally they return different view types, but that shouldn't make a difference for the user, shouldn't it? In particular, consider the following situation:
struct YellowCard : View {
@ViewBuilder var body : some View {
		Color.yellow.border(Color.blue).fitCardAspectRatio1()
}
}
struct YellowBoard : View {
private func cardView(size: CGSize) -> some View {
return YellowCard().frame(width: size.width, height: size.height)
}
private func computeDimensions(_ size : CGSize) -> (card : CGSize, size : CGSize) {
let ratio = aspectRatio()
let factor = min(size.width / ratio.width, size.height / ratio.height)
let size = CGSize(width: factor * ratio.width, height: factor * ratio.height)
let cardWidth = size.width / 3
let cardHeight = size.height / 2
let card = CGSize(width: cardWidth, height: cardHeight)
return (card: card, size: size)
}
var body : some View {
GeometryReader { geom in
let dims = computeDimensions(geom.size)
VStack(spacing: 0) {
HStack(spacing: 0) {
ForEach(0 ..< 3) { _ in cardView(size: dims.card) }
}
HStack(spacing: 0) {
ForEach(3 ..< 5) { _ in cardView(size: dims.card) }
}
}
}.aspectRatio(aspectRatio(), contentMode: .fit)
}
func aspectRatio() -> CGSize {
let card = cardAspectRatio!
let width = 3 * card.width
let height = 2 * card.height
return CGSize(width: width, height: height)
}
}
This behaves as expected, when for example running this on MacCatalyst and resizing the window with just a YellowBoard in it, the board adapts to the sizes. But when using fitCardAspectRatio2 or fitCardAspectRatio3 instead in the definition of YellowCard, the board will not adapt its size but just keep its initial size.
That surely is a bug? Or is this behaviour somehow to be expected?
Post
Replies
Boosts
Views
Activity
I am designing an algorithm for which I need to know how many threads can run simultaneously on the GPU. The app runs on iOS, but I am also running test code via MacCatalyst on macOS. I found that on macOS for the "AMD Radeon Pro Vega 64" GPU the maximum number of simultaneous threads is 16384 by timing it. Is there a reliable way to find out this number via the Metal API? Or do I need to run some dummy tasks and time them to find it out?
So I have written this simple app that lets a user open a folder, and then my app allows the user to create subdirectories and text files in this folder. I am using NSFileCoordinator to coordinate.
Now, when the folder the user opens is on iCloud Drive (in particular, in my Documents folder), the text files that I create on my Mac are not visible on my iPad in my App. To make them visible, I need to open the Files App, navigate to the folder that contains the text file, and then the Files App downloads these text files, which become then also visible in my App.
On the other hand, when I create text files on the iPad, they are immediately visible on the Mac.
There is no such problem with the subdirectories.
Is this a known behaviour? Can it somehow be fixed?
Does anyone have an idea how to let the user open a folder in an app built with UIKit for Mac?My use case is that I want the user to be able to specify a directory on the Mac, from where the app will import data into the app.I don't seem to be able to use NSOpenPanel when building for UIKit for Mac. Is there a workaround??Any help much appreciated.
I am confused about how to program modular components for use with UIKit. According to the official documentation, ViewControllers seem to be the way to modularize UI components. So say I write a component as a ViewController. Now, how do I use this component within some view? Is there an official way to do this? All I can find is how to use a ViewController within other ViewControllers.