gpt4 book ai didi

ios - 在 Swift 中禁用动态类型

转载 作者:搜寻专家 更新时间:2023-10-31 08:32:05 25 4
gpt4 key购买 nike

我有一个基于 Sprite Kit 的游戏,它在其中一个场景中使用 UIView,我这样做是为了利用 UITableViewController 来显示游戏设置屏幕。

我遇到的困难是,当用户将他们的 iPad 系统辅助功能设置为使用(超)大字体时,UITableView 中的文本对于单元格来说太大了,看起来很傻。

我想做的是直接禁用应用程序中的动态类型,以便它始终在单元格中显示相同大小的类型。

我发现了另一个类似的帖子 ( here ),但响应提供了 Objective-C 响应:

#import <objc/runtime.h>

@implementation AppDelegate

NSString* swizzled_preferredContentSizeCategory(id self, SEL _cmd) {
return UIContentSizeCategoryLarge; // Set category you prefer, Large being iOS' default.
}

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
Method method = class_getInstanceMethod([UIApplication class], @selector(preferredContentSizeCategory));
method_setImplementation(method, (IMP)swizzled_preferredContentSizeCategory);

...
}

我需要在 Swift 中执行此操作。

在 Xcode 7+ 的 Swift 中做同样事情的正确方法是什么?

最佳答案

谢谢 @zeeple为解决方案。这是原始问题的答案:

“preferredContentSizeCategory”在Objective-C中是一个方法,但在Swift中它是一个只读变量。

所以在你的 AppDelegate 中是这样的:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

// MARK: - UIApplicationDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

UIApplication.classInit

self.window = UIWindow(frame: UIScreen.main.bounds)
...
self.window?.makeKeyAndVisible()
return true
}
}

// MARK: - Fix Dynamic Type

extension UIApplication {

static let classInit: Void = {
method_exchangeImplementations(
class_getInstanceMethod(UIApplication.self, #selector(getter: fixedPreferredContentSizeCategory))!,
class_getInstanceMethod(UIApplication.self, #selector(getter: preferredContentSizeCategory))!
)
}()

@objc
var fixedPreferredContentSizeCategory: UIContentSizeCategory {
return .large
}
}

关于ios - 在 Swift 中禁用动态类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38711280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com