1

I am using this code for help Xcode to build my Window Size as big as the screen of mac, but it does not get maxed! How we can read the size of screen and set it for view size for taking all available space on screen? It would be great getting a SwiftUI approach for the issue.

struct ContentView: View {

    var body: some View {
        
        Color.purple
            .frame(maxWidth: .infinity, maxHeight: .infinity)

    }
    
}
Marcy
  • 4,611
  • 2
  • 34
  • 52
ios coder
  • 1
  • 4
  • 31
  • 91

2 Answers2

0
    let screenSize = NSScreen.main?.frame.size ?? CGSize(width: 800, height: 600)
    
    Color.purple
        .frame(width: screenSize.width, height: screenSize.height)
ChrisR
  • 9,523
  • 1
  • 8
  • 26
0

Here's a cross-platform version I've created, please let me know if you spot anything amiss.

It's not yet tested on MacOS or WatchOS but compiles fine.

import Foundation

#if os(macOS)
import AppKit
#elseif os(iOS)
import UIKit
#elseif os(watchOS)
import WatchKit
#endif

struct Screen {
#if os(macOS)
    static var size: CGSize = NSScreen.main?.visibleFrame.size ?? NSScreen.main?.frame.size ?? CGSizeMake(0, 0)
#elseif os(iOS)
    static var size: CGSize = UIScreen.main.bounds.size
#elseif os(watchOS)
    static var size: CGSize = WKInterfaceDevice.current().screenBounds.size
#endif
    static var minX: CGFloat { 0 }
    static var minY: CGFloat { 0 }
    static var maxX: CGFloat { size.width }
    static var maxY: CGFloat { size.height }
    static var midX: CGFloat { (maxX - minX) / 2 }
    static var midY: CGFloat { (maxY - minY) / 2 }
}
kwiknik
  • 570
  • 3
  • 7