ios - 键盘底角图标在iOS中改变颜色

标签 ios objective-c xcode uikeyboard

如果我全局更新所有按钮以具有相同的颜色,我不希望我的键盘上的按钮改变颜色。如何手动控制它以重置为原始颜色?

[[UIApplication sharedApplication] delegate].window.tintColor = [Helper getColor:color];
    [[UIButton appearance] setBackgroundColor:[Helper getColor:color]];

应用委托(delegate)
@interface ExposureDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, strong) IBOutlet ExposureWindow *window;
@property (nonatomic, strong) EventsViewController *eventsViewController;

@end

@implementation ExposureDelegate

@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


      self.window = [[ExposureWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self.window setRootViewController:menuViewController];

        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(ExposureWindow *w in windows) {
            NSLog(@"window: %@",window.description);
            if(w.rootViewController == nil){
                UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
                w.rootViewController = vc;
            }
        }

        [self.window makeKeyAndVisible];

enter image description here

最佳答案

您可以定义哪些 View /类应该尊重 UIAppearance使用规则:

+ (instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray<Class<UIAppearanceContainer>> *)containerTypes;

(Relevant Documentation)

现在可能有几种方法可以做到这一点,这只是其中一种。如果你想要你的答案,你可以跳到最后,但我提供了更多信息来理解为什么以及如何好奇。

使用此方法的更多相关讨论:

请记住,您传递的数组不是您希望外观代理工作的所有位置的列表,如果您在同一个列表中传递多个,它将​​被评估为第一个元素嵌套最多的层次结构,并且最后一个元素是最外层的。

又名
[UIButton appearanceWhenContainedInInstancesOfClasses:@[[MyView class], [UITableView class]]] 

表示仅将规则应用于 MyView 内的按钮位于 UITableView 内的的。

另一个要理解的好事情是如何评估规则。UIKit将始终首先检查最长的层次结构。它总是会开始检查最外面的对象(在大多数情况下,像 UIViewController )。

外观将应用于匹配的第一个层次结构。

又名
UILabel.appearance(whenContainedInInstancesOf: [CustomView.self]).textColor = red;
UILabel.appearance(whenContainedInInstancesOf: [UIViewController.self]).textColor = UIColor.gray;

这意味着您的所有标签都将显示为灰色,因为它开始检查最外面的对象 UIViewController并且由于那是一场比赛,它停止进一步检查。

但是因为首先评估更长的层次结构:
UILabel.appearance(whenContainedInInstancesOf: [CustomView.self, UIViewController.self]).textColor = red;
UILabel.appearance(whenContainedInInstancesOf: [UIViewController.self]).textColor = UIColor.gray;

表示标签将在 CustomView 上显示为红色正如预期的那样,因为第一行在其层次结构中有两个元素,这意味着它将在所有一个元素层次结构(如第二行)之前进行检查。

最后你的答案:

所以我们所要做的就是在键盘窗口的堆栈中的某处找到一个父 View 或 View Controller - 并在其上设置一个清晰的背景颜色......

除了检查键盘窗口的完整层次结构后,每个 Controller 和通往这些按钮的 View 都​​是私有(private)的:
enter image description here
(我检查了从 UIRemoteKeyboardWindow 开始的每个 View 的右侧可见的类层次结构。它们都是私有(private)的)

由于我们无法触及给我们留下较少选项的系统类 - 我们必须设置仅适用于 UIWindow 的规则。或 UIViewControllers我们的主应用程序窗口。

我可以想象一个解决方案,我有一个自定义 UIViewController类名为 BaseController我所有的 VC 都继承自.. 但这似乎很烦人。我们为什么不创建一个自定义 UIWindow而是在我们自己的应用程序中使用的类:

(这是在 Swift 中,因为它对我来说更容易)
// internal and final just included for compiler optimizations, not necessary
internal final class AppWindow: UIWindow {}

@UIApplicationMain
internal final class AppDelegate: UIResponder, UIApplicationDelegate {

    lazy var appWindow: AppWindow = {
        return AppWindow(frame: UIScreen.main.bounds)
    }()

...

// then wherever you're setting the key window make sure you use your custom one
appWindow.rootViewController = vc
appWindow.makeKeyAndVisible()

现在您可以将规则应用到 AppWindow :
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[[AppWindow class]]]
           setBackgroundColor:[Helper getColor:color]];

(你应该对色调的东西没问题,因为你已经写了 bc 已经只影响正在使用应用程序的窗口)

关于ios - 键盘底角图标在iOS中改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59187408/

相关文章:

swift - 在 Xcode 9.2 中播放声音

ios - 在 iPhone plus 设备上点击横向媒体选择按钮时 AVPlayerViewController 崩溃

ios - iOS 上的 CrossDeviceInfo.Current.Id 不一致

ios - 如何在命令中使用字符串变量值作为以该字符串命名的标签?

ios - 了解自动调整属性和 mask

ios - 应用程序中带有 TabBarController 的导航 Controller

ios - 如何实现 SwiftyJSON 来获取这些数据

objective-c - 如何自定义UIActionSheet?苹果系统

iPhone App : How to get default value from root. plist?

ios - swift 3 : Caching images in a collectionView