J'avais UIActionSheet
code suivant pour changer la couleur de text des articles que j'ajoute dans UIActionSheet
.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet { for (UIView *subview in actionSheet.subviews) { if ([subview isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton *)subview; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } } }
Cela fonctionne très bien dans iOS7
, mais dans iOS8
le code ci-dessus ne change pas la couleur du text des éléments.
Maintenant, ma question est de savoir comment changer la couleur du text des éléments que j'ajoute dans UIActionSheet
utilisant iOS8
??
Information additionnelle:
J'ai ajouté un point d'arrêt pour voir si les lignes suivantes du code ci-dessus sont exécutées ou non:
UIButton *button = (UIButton *)subview; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
Ces lignes ne sont pas exécutées. Donc, if([subview isKindOfClass:[UIButton class]])
est toujours false
pour tous les éléments de actionSheet.subviews
. Donc ce que je pense, c'est que la vue hiérarchique de UIActionSheet
a été modifiée.
Il y a un moyen facile si vous voulez toujours utiliser UIActionSheet
au lieu de UIAlertController
afin de prendre en charge les anciennes versions d'iOS.
UIActionSheet
utilise réellement UIAlertController
dans iOS 8, et il a une propriété privée _alertController
.
SEL selector = NSSelectorFromSsortingng(@"_alertController"); if ([actionSheet respondsToSelector:selector]) { UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"]; if ([alertController isKindOfClass:[UIAlertController class]]) { alertController.view.tintColor = [UIColor blueColor]; } } else { // use other methods for iOS 7 or older. }
Pour modifier la couleur du titre du button, vous devez utiliser UIAlertController
et définir le tintColor
de la view
principale.
Exemple de définition des colors du titre du button en noir:
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"How would you like to proceed?" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *finish = [UIAlertAction actionWithTitle:@"Finish" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self performSelector:@selector(finish:) withObject:nil]; }]; UIAlertAction *playAgain = [UIAlertAction actionWithTitle:@"Play Again" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self performSelector:@selector(playAgain:) withObject:nil]; }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { [actionSheetController dismissViewControllerAnimated:YES completion:nil]; }]; [actionSheetController addAction:finish]; [actionSheetController addAction:playAgain]; [actionSheetController addAction:cancel]; //******** THIS IS THE IMPORTANT PART!!! *********** actionSheetController.view.tintColor = [UIColor blackColor]; [self presentViewController:actionSheetController animated:YES completion:nil];
Ok, j'ai rencontré le même problème mais je pense avoir trouvé une solution:
La manière appropriée devrait être comme ceci et je suppose que cela fonctionne dans iOS7:
[[UIButton appearanceWhenContainedIn:[UIActionSheet class], nil] setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Mais cela ne fonctionnera pas dans iOS8 en raison du fait que les "buttons" ActionSheets sont maintenant basés sur un UICollectionView. Donc, après avoir creusé autour de moi, j'ai eu ce travail à la place:
[[UICollectionView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]];
Je l'utilise.
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]];
Ajouter une ligne (AppDelegate) et fonctionne pour tous UIAlertController.
J'ai la même tâche et j'ai fait ce hack aujourd'hui, sale, mais ça marche
class CustomAlertViewController: UIAlertController { internal var cancelText: Ssortingng? private var font: UIFont? = UIFont(name: "MuseoSansCyrl-500", size: 12) override func viewDidLoad() { super.viewDidLoad() self.view.tintColor = UIColor.blackColor() } override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() self.findLabel(self.view) //if you need ios 9 only, this can be made in viewWillAppear } func findLabel(scanView: UIView!) { if (scanView.subviews.count > 0) { for subview in scanView.subviews { if let label: UILabel = subview as? UILabel { if (self.cancelText != nil && label.text == self.cancelText!) { dispatch_async(dispatch_get_main_queue(),{ label.textColor = UIColor.redColor() //for ios 8.x label.tintColor = UIColor.redColor() //for ios 9.x }) } if (self.font != nil) { label.font = self.font } } self.findLabel(subview) } } } }
Changer la tintColor
de la window
fonctionné pour moi.
Dans l'application - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method
écrire cela:
[self.window setTintColor:[UIColor redColor]];
Cela a changé la couleur de police de tous les objects UIActionSheet
.
Pour iOS 7 rétrocompatibilité, et pour collecter la couleur de teinte par défaut appropriée (que Apple pourrait bien changer dans les futures versions), je l'utilise. Merci aux réponses sur la teinte par défaut et à la réponse de Lucas ci-dessus.
const Class alertControllerClass = NSClassFromSsortingng(@"UIAlertController"); if(alertControllerClass) { UIColor *defaultTintColour = UIView.new.tintColor; [[UIView appearanceWhenContainedIn: alertControllerClass, nil] setTintColor: defaultTintColour]; }
Attention cependant – vous devez vous assurer que vous l'utilisez avant de commencer à définir la teinte globale sinon UIView.new.tintColor
vous donnera simplement la teinte en cours que vous avez configurée.