【问题标题】:Why do I see the text late when I move the screen on Xcode?为什么我在 Xcode 上移动屏幕时看到文本较晚?
【发布时间】:2019-09-05 02:26:22
【问题描述】:

我是iOS 开发的初学者。开发当前项目时出现问题。移动屏幕时字符会显示得太晚。我刚刚运行了一个正常的屏幕切换。为什么会这样?

startViewController.swift

    @IBAction func Onclick(_ sender: Any) {
        AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
            if response {
                print(response , ": granted")
                let center = UNUserNotificationCenter.current()
                // Request permission to display alerts and play sounds.
                center.requestAuthorization(options: [.alert, .sound])
                { (granted, error) in
                    if granted {
                        print(granted, ": is granted")
                        self.moveScreen()
                    } else {
                        print(granted, ": is not granted")
                        self.moveScreen()
                    }
                }
            } else {
                print(response , ": not granted")
                let center = UNUserNotificationCenter.current()
                // Request permission to display alerts and play sounds.
                center.requestAuthorization(options: [.alert, .sound])
                { (granted, error) in
                    if error == nil {
                        if granted == true {
                             print(granted, ": is granted")
                             self.moveScreen()
                        }
                        else {
                            print(granted, ": is not granted")
                            self.moveScreen()
                        }
                    } else {
                        print(error as Any)
                        }
                }

            }
        }
    }

    func moveScreen(){
        let mainViewScreen = self.storyboard?.instantiateViewController(withIdentifier: "MainViewController")
        mainViewScreen?.modalTransitionStyle = UIModalTransitionStyle.coverVertical
        self.present(mainViewScreen!, animated: true, completion: nil)
    }

MainViewController.swift

import Foundation
import UIKit


class MainViewController: UIViewController {

    @IBOutlet weak var mainLogo: UIImageView!
    @IBOutlet weak var mainText: UITextView!
    @IBOutlet weak var mainSecondText: UITextView!
    @IBOutlet weak var takeWalletText: UIButton!
    @IBOutlet weak var makeWalletText: UIButton!


    @IBAction func takeWallet(_ sender: Any) {
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        changeFontSize()
    }

    func changeFontSize() {
        let screenWidth = UIScreen.main.bounds.size.width
        let screenHeight = UIScreen.main.bounds.size.height

        if screenWidth < 375 {
            // iPhone 4inch and 3.5inch. Smaller than iPhone 8
            // Call change font
            mainText.font = UIFont.systemFont(ofSize: 16)
            mainSecondText.font = UIFont.systemFont(ofSize: 12)

            takeWalletText.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            makeWalletText.titleLabel?.font = UIFont.systemFont(ofSize: 14)

        }
        if screenHeight > 667 {

            mainText.font = UIFont.systemFont(ofSize: 20)
            mainSecondText.font = UIFont.systemFont(ofSize: 16)

            takeWalletText.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            makeWalletText.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        }
    }

}

低加载屏幕

故事板

我在屏幕上向右拖动后被运行,点击,点击一个show后在屏幕上释放移动。

提前致谢

【问题讨论】:

  • 您正在从通知权限请求的完成处理程序中调用moveScreen - 您需要将其分派到主队列DispatchQueue.main.async { self.moveScreen()} - 您应该尽量避免编写使用屏幕尺寸的代码 - 它随着新设备的发布,变得难以维护。您还应该支持动态类型,而不是使用固定的字体大小。
  • 谢谢@Paulw11。 DispatchQueue.main.async 是工作!!!!默认情况下,移动文档中没有此类内容。我们为什么要这样做?我的代码有问题吗?
  • 还有@Paulw11。你说你应该支持动态类型而不是使用固定的字体大小,你怎么能这样做?请给我你的代码好吗?
  • 不幸的是,这不是“请给我代码”的情况。为了创建好的应用程序,需要了解 iOS 平台。有很多来自 Apple 的精彩视频,来自以前的 WWDC 和其他教程 - 这可以让你开始了解什么是动态类型以及为什么要使用它 -developer.apple.com/documentation/uikit/uifont/…
  • 如果是这样,在移动基本文档的屏幕时不要使用异步。我为什么要使用这个?我的代码错了吗?

标签: ios swift xcode navigation storyboard


【解决方案1】:

我在 @Paulw11 的帮助下解决了这个问题。我的问题是,当我设置 NSNotification 时,我将其称为异步请求。这就是为什么我的屏幕延迟了短信迟到的原因。这是通过异步交换函数执行来解决的。

                center.requestAuthorization(options: [.alert, .sound])
                { (granted, error) in
                    if granted {
                        print(granted, ": is granted")
                        DispatchQueue.main.async { self.moveScreen()}
                    } else {
                        print(granted, ": is not granted")
                        DispatchQueue.main.async { self.moveScreen()}
                    }
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2021-01-19
    相关资源
    最近更新 更多