SwiftUI如何调整不同的屏幕大小(高度)
swiftui是否有可能与不同尺寸的iphone(7/8/Xs/iphone 11…)匹配 我目前正在学习swiftui,遇到了一个问题。 如果我这样离开屏幕,新iPhone的屏幕就不好看了。 我正在考虑是否在swiftui上进行响应性测试,以使程序与图片大小相适应。 但是怎么做呢? 对不起,我不适合英语::: 这是密码SwiftUI如何调整不同的屏幕大小(高度),swiftui,autoresize,Swiftui,Autoresize,swiftui是否有可能与不同尺寸的iphone(7/8/Xs/iphone 11…)匹配 我目前正在学习swiftui,遇到了一个问题。 如果我这样离开屏幕,新iPhone的屏幕就不好看了。 我正在考虑是否在swiftui上进行响应性测试,以使程序与图片大小相适应。 但是怎么做呢? 对不起,我不适合英语::: 这是密码 var w = UIScreen.main.bounds.width let h = UIScreen.main.bounds.width struct PrayTimeVi
var w = UIScreen.main.bounds.width
let h = UIScreen.main.bounds.width
struct PrayTimeView: View {
let prayTimes = Bundle.main.decode("time1.json")
var manager = LocalNotificationManager()
var body: some View {
VStack {
VStack(spacing:0) {
Image("Prayer")
.resizable()
.overlay(ImageOverlay(items: prayTimes))
.frame( height: h < 700 ? 210 : 220)
HStack{
Rectangle().fill(Color("Bg"))
.overlay(
VStack{
Text(Date().DDMMYYYY)
.font(h < 700 ? .headline : .title)
//.fontWeight(h < 700 ? .bold : .regular)
.foregroundColor(.white)
Text(Date().DDMMYYYYIslamic)
.font(h < 700 ? .headline : .title)
//.fontWeight(h < 700 ? .bold : .regular)
.foregroundColor(.white)
}
)
}
.frame(height: h < 700 ? 45 : 80)
}
Spacer()
VStack{
GeometryReader {_ in
VStack{
timeView(self.prayTimes)
}
}
}
}.modifier(CustomMudifier())
.navigationBarTitle("Gebet")
}
}
struct PrayTimeView_Previews: PreviewProvider {
static var previews: some View {
PrayTimeView()
}
}
struct ourView: View {
@EnvironmentObject var share : Share
@State var onN = false{
didSet{
share.isOn = onN
if onN{
print("ON")
//set notifications
let manager = LocalNotificationManager()
// manager.sheduleForDay(item : item )
manager.scheduleForAll(items: items)
}
else{
print("off")
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}
UserDefaults.standard.set(onN, forKey: "on")
}
}
var items: [PrayTime]
var item: PrayTime
var body: some View {
let bonN = Binding<Bool>(get: {self.onN}, set:{self.onN = $0})
return
GeometryReader{ geo in
VStack {
Toggle(" Notifications On", isOn: bonN)
.frame(height:20)
List{
Rectangle().fill(Color.clear).frame(height: (geo.size.height - 50)/8)
.overlay(
HStack{
Text("Fajr")
.fontWeight(self.share.currentPrayerIndex == 0 ? .heavy : .regular)
Spacer()
Text(self.item.fajr)
Image(systemName: "speaker")
.padding(.vertical, h < 700 ? 0 : 17)
.padding(.horizontal)
}
)
Rectangle().fill(Color.clear).frame(height: (geo.size.height - 50)/8)
.overlay(
HStack{
Text("Sunrise")
Spacer()
Text(self.item.sunrise)
Image(systemName: "circle")
.padding(.vertical, h < 700 ? 0 : 17)
.padding(.horizontal)
}
)
Rectangle().fill(Color.clear).frame(height: (geo.size.height - 50)/8)
.overlay(
HStack{
Text("Dhuhr")
.fontWeight(self.share.currentPrayerIndex == 1 ? .heavy : .regular)
Spacer()
Text(self.item.dhuhr)
Image(systemName: "speaker")
.padding(.vertical, h < 700 ? 0 : 17)
.padding(.horizontal)
}
)
Rectangle().fill(Color.clear).frame(height: (geo.size.height - 50)/8)
.overlay(
HStack{
Text("Asr")
.fontWeight(self.share.currentPrayerIndex == 2 ? .heavy : .regular)
Spacer()
Text(self.item.asr)
Image(systemName: "speaker")
.padding(.vertical, h < 700 ? 0 : 17)
.padding(.horizontal)
}
)
Rectangle().fill(Color.clear).frame(height: (geo.size.height - 50)/8)
.overlay(
HStack{
Text("Maghrib")
.fontWeight(self.share.currentPrayerIndex == 3 ? .heavy : .regular)
Spacer()
Text(self.item.maghrib)
Image(systemName: "speaker")
.padding(.vertical, h < 700 ? 0 : 17)
.padding(.horizontal)
}
)
Rectangle().fill(Color.clear).frame(height: (geo.size.height - 50)/8)
.overlay(
HStack{
Text("Isha")
.fontWeight(self.share.currentPrayerIndex == 4 ? .heavy : .regular)
Spacer()
Text(self.item.isha)
Image(systemName: "speaker")
.padding(.vertical, h < 700 ? 0 : 17)
.padding(.horizontal)
}
)
}
.disabled(w < 350 ? false : true)
.foregroundColor(Color.black)
.listStyle(GroupedListStyle())
}
}
.onAppear{
self.onN = UserDefaults.standard.bool(forKey: "on")
}
}
}
func timeView(_ items: [PrayTime]) -> AnyView {
let date = Date()
let day = date.day + "-" + date.month
for item in items{
if (item.id == day) {
return(AnyView(ourView(items: items, item: item)))
}
}
return AnyView(Text("Nothing Found. Please update to resolve."))
}
struct CountDownView: View {
@EnvironmentObject var share: Share
var items:[PrayTime]
var manager = LocalNotificationManager()
@State var interval : Int = 0
let prayers = ["Fajr","Dhuhr","Asr","Maghrib","Isha"]
@State var currentPrayerIndex = 0
var timerText : String {
let dateForm = DateComponentsFormatter()
dateForm.zeroFormattingBehavior = .pad
dateForm.unitsStyle = .positional
dateForm.allowedUnits = [.hour, .minute, .second ]
return dateForm.string(from: TimeInterval(interval)) ?? "00:00"
}
@State var timer = Timer.publish(every: 1, tolerance: 0.5, on: .main, in: .common).autoconnect()
var body: some View {
HStack{
Text("(-\(timerText))").font(Font.title.monospacedDigit()).bold()
.foregroundColor(.white)
// .frame(width:150)
Text(prayers[currentPrayerIndex])
.font(h < 700 ? .title : .largeTitle).bold()
.foregroundColor(.white)
}
.onAppear{self.interval = self.countdownTimer(items: self.items)
}
.onReceive(timer) { time in
self.interval -= 1
if self.interval == 1 {
self.timer.upstream.connect().cancel()
Sounds.playSoundsLocal(soundfile: "cbcd8d249dcc.mp3")
DispatchQueue.main.asyncAfter(deadline: .now() + Double(5)){
self.interval = self.countdownTimer(items: self.items)
self.timer = Timer.publish(every: 1, tolerance: 0.5, on: .main, in: .common).autoconnect()
}
}
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
self.timer.upstream.connect().cancel()
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
self.interval = self.countdownTimer(items: self.items)
self.timer = Timer.publish(every: 1, tolerance: 0.5, on: .main, in: .common).autoconnect()
if self.share.isOn{
self.manager.scheduleForAll(items: self.items)
}
}
}
func countdownTimer(items: [PrayTime]) -> Int {
let date = Date()
let day = date.day + "-" + date.month
for item in items{
if (item.id == day) {
let notifTimeFajr = date.createDateFromJsonIdAndTime(dateStrTime: item.fajr, dayAndMonthStringJsonId: item.id) ?? Date()
let notifTimeDhuhr = date.createDateFromJsonIdAndTime(dateStrTime: item.dhuhr, dayAndMonthStringJsonId: item.id) ?? Date()
let notifTimeAsr = date.createDateFromJsonIdAndTime(dateStrTime: item.asr, dayAndMonthStringJsonId: item.id) ?? Date()
let notifTimemAghrib = date.createDateFromJsonIdAndTime(dateStrTime: item.maghrib, dayAndMonthStringJsonId: item.id) ?? Date()
let notifTimeIsha = date.createDateFromJsonIdAndTime(dateStrTime: item.isha, dayAndMonthStringJsonId: item.id) ?? Date()
let dayPrayerTimesArray = [notifTimeFajr,notifTimeDhuhr,notifTimeAsr,notifTimemAghrib,notifTimeIsha]
for i in 0..<dayPrayerTimesArray.count{
if dayPrayerTimesArray[i] > Date(){
interval = Int(dayPrayerTimesArray[i].timeIntervalSince(Date()))
currentPrayerIndex = i
share.currentPrayerIndex = i
return interval
}
}
}
}
return 0
}//end countdown function
}
struct ImageOverlay: View {
var items:[PrayTime]
@State var date = Date()
var body: some View {
VStack{
VStack {
Text("\(timeString(date: date))").bold()
.onAppear(perform: {let _ = self.updateTimer
})
.font(h < 700 ? .title : .largeTitle)
.padding(6)
.foregroundColor(.white)
}
VStack{
CountDownView(items: items)
}
}
}
var timeFormat: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm a"
return formatter
}
func timeString(date: Date) -> String {
let time = timeFormat.string(from: date)
return time
}
var updateTimer: Timer {
Timer.scheduledTimer(withTimeInterval: 1, repeats: true,
block: {_ in
self.date = Date()
})
}
}
var w=UIScreen.main.bounds.width
设h=UIScreen.main.bounds.width
结构PrayTimeView:视图{
让prayTimes=Bundle.main.decode(“time1.json”)
var manager=LocalNotificationManager()
var body:一些观点{
VStack{
VStack(间距:0){
图像(“祈祷”)
.可调整大小()
.覆盖图(图像覆盖图(项目:prayTimes))
.框架(高度:h<700?210:220)
HStack{
矩形().填充(颜色(“Bg”))
.覆盖(
VStack{
文本(日期().DDMMYYYY)
.font(h<700?。标题:。标题)
//.fontWeight(h<700?.bold:.常规)
.foregroundColor(.白色)
文本(日期().DDMMYYYYIslamic)
.font(h<700?。标题:。标题)
//.fontWeight(h<700?.bold:.常规)
.foregroundColor(.白色)
}
)
}
.框架(高度:h<700?45:80)
}
垫片()
VStack{
GeometryReader{uu}in
VStack{
时间视图(自我实践)
}
}
}
}.modifier(CustomMudifier())
.navigationBarTitle(“Gebet”)
}
}
结构PrayTimeView\u预览:PreviewProvider{
静态var预览:一些视图{
PrayTimeView()
}
}
结构视图:视图{
@环境对象变量共享:共享
@状态变量onN=false{
迪塞特{
share.isOn=onN
如果是{
打印(“在”)
//设置通知
让manager=LocalNotificationManager()
//经理日程安排日期(项目:项目)
manager.scheduleForAll(项目:项目)
}
否则{
打印(“关闭”)
UnuseNotificationCenter.current()。removeAllPendingNotificationRequests()
}
UserDefaults.standard.set(onN,forKey:“on”)
}
}
变量项:[练习时间]
变量项:PrayTime
var body:一些观点{
让bonN=Binding(get:{self.onN},set:{self.onN=$0})
返回
GeometryReader{geo-in
VStack{
切换(“通知打开”,isOn:bonN)
.框架(高度:20)
名单{
矩形().fill(Color.clear).frame(高度:(geo.size.height-50)/8)
.覆盖(
HStack{
文本(“Fajr”)
.fontWeight(self.share.CurrentPruerIndex==0?.heavy:.常规)
垫片()
文本(self.item.fajr)
图像(系统名称:“扬声器”)
.填充(.垂直,h<700?0:17)
.padding(.卧式)
}
)
矩形().fill(Color.clear).frame(高度:(geo.size.height-50)/8)
.覆盖(
HStack{
文本(“日出”)
垫片()
文本(self.item.sunrise)
图像(系统名称:“圆圈”)
.填充(.垂直,h<700?0:17)
.padding(.卧式)
}
)
矩形().fill(Color.clear).frame(高度:(geo.size.height-50)/8)
.覆盖(
HStack{
文本(“Dhuhr”)
.fontWeight(self.share.CurrentPruerIndex==1?.heavy:.常规)
垫片()
文本(self.item.dhuhr)
图像(系统名称:“扬声器”)
.填充(.垂直,h<700?0:17)
.padding(.卧式)
}
)
矩形().fill(Color.clear).frame(高度:(geo.size.height-50)/8)
.覆盖(
HStack{
文本(“Asr”)
.fontWeight(self.share.CurrentPruerIndex==2?.heavy:.常规)
垫片()
文本(self.item.asr)
图像(系统名称:“扬声器”)
.填充(.垂直,h<700?0:17)
.padding(.卧式)
}
)
矩形().fill(Color.clear).frame(高度:(geo.size.height-50)/8)
.覆盖(
HStack{
文本(“Maghrib”)
.fontWeight(self.share.CurrentPruerIndex==3?.heavy:.常规)
垫片()
文本(self.item.maghrib)
图像(系统名称:“扬声器”)
.填充(.垂直,h<700?0:17)
.padding(.卧式)
}
)
矩形().fill(Color.clear).frame(高度:(geo.size.height-50)/8)
.覆盖(
HStack{
文本(“Isha”)
.fontWeight(self.share.CurrentPruerIndex==4?.heavy:.常规)
垫片()
文本(self.item.isha)
图像(系统名称:“扬声器”)
.填充(.垂直,h<700?0:17)
.padding(.卧式)
}
)
}
.禁用(w<350?错误:正确)