swiftui是否有可能匹配不同尺寸的iPhone(7/8 / Xs / iphone 11 ....)?
我目前正在学习swiftui,遇到一个问题。 如果我这样离开屏幕,那么新iPhone的屏幕就不好了。 我正在考虑是否根据程序的大小在程序中进行swiftui的响应。 但是如何? 抱歉,我不适合英语:::
这是代码
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()
})
}
}
答案 0 :(得分:0)
这似乎不是SwiftUI问题。如果您的iPhone X或更高版本的屏幕像iPhone 8一样被切割(带有旧的主页按钮)(如屏幕截图所示),请尝试检查并调整您的启动图像(1125×2436 px iPhone X),如果您有{{ 1}}(推荐),或在LaunchImage.launchimage
中重设约束。
启动图像会影响显示。 Read more。