Skip to content

Instantly share code, notes, and snippets.

@oocoocococo
Last active September 8, 2020 07:20
Show Gist options
  • Save oocoocococo/0ddb98e5361fc207b402303e457f009a to your computer and use it in GitHub Desktop.
Save oocoocococo/0ddb98e5361fc207b402303e457f009a to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("ドッグマッチングアプリ")
.font(.subheadline)
.opacity(0.8)
Text("わんだー")
.font(.title)
.fontWeight(.black)
.foregroundColor(.pink)
.padding(5)
.shadow(color: .gray, radius: 1, x: 1, y: 1)
Image("Dog001")
.resizable()
.scaledToFit()
.frame(width: 220)
.padding()
.hiddenOnSmallDevices() // 小さいデバイスでは非表示にする
Button("アプリを開始する", action: {})
.frame(width: UIScreen.main.bounds.width * 0.8)
.padding()
.foregroundColor(.white)
.background(Color.pink)
.cornerRadius(10)
.padding(.top, 40)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
view(device: "iPhone SE (1st generation)")
view(device: "iPhone 11 Pro")
}
}
static func view(device: PreviewDevice) -> some View {
ContentView()
.previewDevice(device)
}
}
/// 小さいデバイスでは非表示にする
struct HiddenOnSmallDevices: ViewModifier {
func body(content: Content) -> some View {
Group {
if UIScreen.main.nativeBounds.height <= 1334 {
// 4.7インチ以下の場合はサイズを0にして隠す
content
.frame(width: 0, height: 0)
.hidden()
} else {
// それ以外の場合はそのまま表示
content
}
}
}
}
extension View {
/// 小さいデバイスでは非表示にする
/// - Returns: View
func hiddenOnSmallDevices() -> some View {
self.modifier(HiddenOnSmallDevices())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment