Is it possible to change the background color of UIAlertController? I have managed to change the background color of UIAlertView. I have gone through third parties also to do the same, but I want o do it without use of any third party.
Below is my code:
UIAlertController * alert=[UIAlertController alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIView *firstSubview = alert.view.subviews.firstObject;
UIView *alertContentView = firstSubview.subviews.firstObject;
for (UIView *subSubView in alertContentView.subviews) {
subSubView.backgroundColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
//Close Action
}];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:#"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
//Close Action
}];
[cancelAction setValue:[UIColor greenColor] forKey:#"titleTextColor"];
[deleteAction setValue:[UIColor redColor] forKey:#"titleTextColor"];
[alert addAction:cancelAction];
[alert addAction:deleteAction];
[self presentViewController:alert animated:YES completion:nil];
Thanks in advance!!
Related
in my app i have tableview and for clear all cells i given an action to clear button.
button work correctly and all cell deleted. but i want to give a option to user to canceled
i used UIAlertController class and its work only one time after i chosen delete button.(its work every time if i chosen cancel button)
warning in console:
Warning:
Attempt to present UIAlertController on HistoryViewController which is already presenting UINavigationController
- (IBAction)showNormalActionSheet:(id)event
{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *resetAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Clear all recent", #"Reset action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
[self onDeleteClick];
NSLog(#"Reset action");
}];
[alertController addAction:resetAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
//- (IBAction)onDeleteClick:(id) event {
- (void)onDeleteClick {
linphone_core_clear_call_logs([LinphoneManager getLc]);
[tableController loadData];
editButton.hidden = ([tableView.dataSource tableView:tableView numberOfRowsInSection:0] == 0);
if(editButton.selected) {
[editButton toggle];
[self onEditClick:nil];
}
}
I'm trying to change the background highlight color for my alertcontroller. For eg in the below image, right now the default background highlight for a cell is light gray, I need to change it to some other color. I saw various post on changing the tint color but I dont need to change the title color only the background highlight. Is this possible?
//This is my current code
- (IBAction)showAlert:(UIButton *)sender {
UIAlertController *alert =[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* save = [UIAlertAction
actionWithTitle:#"Save"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Perform some action
}];
UIAlertAction* saveas = [UIAlertAction
actionWithTitle:#"Save As"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Perform some action
}];
UIAlertAction* discard = [UIAlertAction
actionWithTitle:#"Discard"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
//Perform some action
}];
[alert addAction:save];
[alert addAction:saveas];
[alert addAction:discard];
[alert setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
popPresenter.sourceView = sender;
popPresenter.sourceRect = sender.bounds;
[self presentViewController:alert animated:YES completion:nil];
}
I have made an ActionSheet like this:
Now I want change the color Cancel And Delete to give the feel like they are disabled.
I am able to change the color of all buttons but not individuals.
I have tried using categories but I am keep getting warining that ActionSheet is Depreciated. And the code didn't work also.
I have written this Code:
- (void)setButton:(NSInteger)buttonIndex toState:(BOOL)enabled {
for (UIView* view in self.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
if (buttonIndex == 0) {
if ([view respondsToSelector:#selector(setEnabled:)])
{
UIButton* button = (UIButton*)view;
button.enabled = enabled;
}
}
buttonIndex--;
}
}
}
Is it possible to give disabled look to some of the buttons of Action sheet presented via AlertViewController.
Below is the my code to present Action Sheet:
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:#"Action Sheet" message:#"Using the alert controller" preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet.view setTintColor:[UIColor redColor]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Cancel button tappped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Distructive button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Group 1" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Distructive button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
What you can do is use the setEnabled property of UIAlertAction.
This will render your button as 'greyed out'.
I have modified a piece of your code to incorporate this change:
UIAlertAction *action = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
[action setEnabled:NO];
[actionSheet addAction:action];
Ofcourse, you can't click on the button after you disable it.
Other options would involve using UIAlertActionStyle.
There are three:
UIAlertActionStyleDefault,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
UIAlertActionStyleDefault will render your button with tintColor.
UIAlertActionStyleCancel will render your button on the bottom of the sheet.
UIAlertActionStyleDestructive renders your button with a 'red' color. (Think: Delete button)
Get the actions array and change the state for the action you want.
This way you can change the state even after adding the action.
Here is the sample code.
NSArray* actionlist = actionSheet.actions;
UIAlertAction * action2 = [actionlist objectAtIndex:1];
action2.enabled = false;
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
I found one weird behavior of UIAlertController in Objective-C. I have added two actions in it but I found when user selects any action - handler call back get call after approx 1 sec. I am wondering why this delay is happening to get call back. Is it possible to avoid this by any way?
- (IBAction)openAlert:(id)sender {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Info"
message:#"You are using UIAlertController"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okBtn = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// Delay ~ 1 sec to get control here
self.view.backgroundColor = [UIColor blackColor];
}];
UIAlertAction* cancelBtn = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
}];
[alert addAction:okBtn];
[alert addAction:cancelBtn];
[self presentViewController:alert animated:YES completion:nil];
}
Its on main thread. To make sure I tried below code snippet.
dispatch_async(dispatch_get_main_queue(), ^{
// Alert view code
});
I'm trying to display a UIAlertController with some action buttons. It works fine on the iPhone, because it pops from the bottom of the device. I have a problem with the ipad, the UIAlertController does not center properly. It shows a bit off to the right. I can subtract from the x coordinate by subtracting say 150f. Is there a way to just get it center?
UIAlertController * view= [UIAlertController
alertControllerWithTitle:#"My Title"
message:#"Select your Choice"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
[view addAction:ok];
[view addAction:cancel];
view.popoverPresentationController.sourceView = self.view;
view.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 0.0, 0.0);
[self presentViewController: view animated:YES completion:nil];
If you have simple actions then better to use Alertstyle, it centers automatically or if you insist to use Actionsheet style, try setting popoverPresentationController.permittedArrowDirections = 0 this works nice with a fixed orientation but fails if you rotate in iPad.
// Alert
- (void) showAlert
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"TEst" message:#"test Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}