Customizing the buttons on a UIAlertView - ios

This is the current way i'm customizing my buttons:
UIAlertView *av = [[UIAlertView alloc] init];
[av addButtonWithTitle:#""];
UIButton *yesButton = [av.subviews lastObject];
[av show];
[yesButton setImage:[UIImage imageNamed:#"test.png"] forState:UIControlStateNormal];
The problem with this is that the original view is still visible around the image I set for the button. It doesn't completely encapsulate the image. Here's an example of what I have so far:
https://www.dropbox.com/s/htb9pfihwmel5oo/testimage.png
Is there anyway to make it so that the image completely takes up the entire button?

If you wan't something that's not as robust as Sly Raskal's answer and just want a quick hack, that works similar to your existing code. You could do something like this..Of course you can change the new buttons frame to match how you want it. Also, you would have to define the UIAlertView as a variable to so you have a reference to it, and dismiss it in your someAction: method.
UIAlertView *av = [[UIAlertView alloc] init];
[av addButtonWithTitle:#""];
UIButton *yesButton = [av.subviews lastObject];
[yesButton setHidden:YES];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[av addSubview:button];
[av show];
[button setImage:[UIImage imageNamed:#"test.png"] forState:UIControlStateNormal];
[button addTarget:av action:#selector(someAction:) forControlEvents:[yesButton allControlEvents]];
[button setFrame:yesButton.frame];

If in this case all you are doing is styling the buttons to look different, but in a simple fashion, I would follow this tutorial to accomplish the effect you are looking for:
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/
If the application you are creating is destined for the App Store, I just want to also mention that whatever changes you implement, be sure it doesn't use any private API's or goes against anything documented in the HIG. Either of these circumstances is surely going to red flag your app for rejection.

Related

How to change the duration of SCLAlertView according to response from server in objective c

I am new in iOS and I am facing problem regarding to implement the SCLAlertView for server response. I am using UIActivityIndicatorView for that and my code is like this
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner setCenter:CGPointMake(510,280)];
[self.view addSubview:spinner];
[spinner startAnimating];
and after response from server
[spinner stopAnimating];
But now I want to use the AlertView of SCLAlertView and its code is like this
SCLAlertView *alert = [[SCLAlertView alloc] init];
alert.showAnimationType = SCLAlertViewHideAnimationSlideOutToCenter;
alert.hideAnimationType = SCLAlertViewHideAnimationSlideOutFromCenter;
alert.backgroundType = SCLAlertViewBackgroundBlur;
[alert showWaiting:self title:#"Waiting..."
subTitle:#"You've just displayed this awesome Pop Up View with transparent background"
closeButtonTitle:nil duration:5.0f];
And it output is like this
I need to display this AlertView till I get the data from server. Means I need to stop it in "DidFinishLoading Method". Just like UIActivityIndicatorView. Is it is possible anyone done this. Thanks in Advance!
You can not hide or dismiss the SCLAlertView . Either you can give the duration of alert or you can set "done" button.
There are two way either you add a done button or you can add this method after get the data from server.
alert.isVisible = NO;
Then add this for remove from view.But it is interactive for user.
alert.shouldDismissOnTapOutside = YES;

iOS UIAlertView with photo

I wanted to use UIAlertView to show photo. I´m using code as below for showing the alert, however it doesn't work. It shows the title, some space and button OK nothing more. I have no idea if I´m doing something wrong, because I´m new in iOS.
-(IBAction)ButtonPressed:(id)sender
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"titel"
message:nil
delegate:nil //or self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 85, 50)];
[imageView setImage:#"dog.png"];
[av setValue:imageView forKey:#"accessoryView"];
[av show];
}
#Popeye is correct what he said : UIAlertView is meant to be used as-is and you shouldn't be messing with the view hierarchy this will get your app rejected from the Apple App review process. Please read Apple documentation regarding UIAlertViews. So you should NOT be using setValue: forKey:#"accessoryView" and/or addSubview:. Specific section below
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
Solutions
1) Use 3 party such as CNPPopupController and many other available check this link
OR
2) create of your own.

missing separator line between Actionitems in ActionSheet ios [duplicate]

It could be probably a bug on iOS7. But the last button is not separated from the previous one
As you can see from the image. This happens on both Simulator and device using iOS7 GM.
Does everyone else has the same problem?
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Title"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:#"First", #"Second", #"Third", #"Fourth", nil];
[actionSheet showInView:self.view];
As you can see the code is quite simple.
Any idea on how to fix the problem? Or some third party library I can use instead of UIActionSheet ?
I think ActionSheet requires a cancel button.So you can add the cancel button title.
Another way is: Specify actionSheet's cancelButtonIndex.
For example,in your case, you can add a "Cancel" in otherButtonTitles at index 4 and then specifiy
actionSheet.cancelButtonIndex = 4.
I found a way to make it work on iPhone and iPad in the least hacky way:
Only init the UIActionSheet with a title
Add your buttons
Add a "CANCEL" button at last
set the CancelButtonIndex to that last index
I assume that the missing separator is caused by the cancel button not being recognized as a separate case when adding it first or through the init.
I found that adding a cancel button with an empty string after initialization works. The cancel button won't show up and the separator shows up.
[sheet addButtonWithTitle: #""];
[sheet setCancelButtonIndex: sheet.numberOfButtons - 1];
But this only works for iPad. On iPhone, an empty cancel button shows up, but I found a hacky workaround to make it work. In addition to the above, in willPresentActionSheet add this code in:
NSInteger offset = 55;
CGRect superFrame = actionSheet.superview.frame;
superFrame.origin.y += offset;
[actionSheet.superview setFrame: superFrame];
// hide underlay that gets shifted with the superview
[(UIView*)[[actionSheet.superview subviews] objectAtIndex: 0] removeFromSuperview];
// create new underlay
CGRect underlayFrame = CGRectMake(0, -offset, superFrame.size.width, superFrame.size.height);
UIView* underlay = [[UIView alloc] initWithFrame: underlayFrame];
underlay.alpha = 0.0f;
[underlay setBackgroundColor: [UIColor colorWithWhite: 0.0f alpha: 0.4f]];
[actionSheet.superview insertSubview: underlay atIndex: 0];
// simulate fade in
[UIView animateWithDuration: 0.3f animations:^{
underlay.alpha = 1.0f;
}];
This shifts down the sheet to hide the cancel button off the screen
The simplest fix is to pass #"" to the cancel button title instead of nil during allocation.
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Title"
delegate:self
cancelButtonTitle:#"" // change is here
destructiveButtonTitle:nil
otherButtonTitles:#"First", #"Second", #"Third", #"Fourth", nil];
[actionSheet showInView:self.view];
UIActionSheet *asAccounts = [[UIActionSheet alloc]
initWithTitle:Localized(#"select_an_account")
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles: nil];
for (int i=0; i<[result count]; i++) {
ACAccount *acct = [result objectAtIndex:i];
[asAccounts addButtonWithTitle:[acct username]];
asAccounts.tag = i;
}
[asAccounts addButtonWithTitle:Localized(#"Cancel")];
asAccounts.cancelButtonIndex = result.count;
[asAccounts showInView:self.view];
If you have a cancel button, the last row will be shown. That is the temp fix I am using now. Do not know any solution if you do not want a cancel button to show
It seems that the initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles: method is buggy and you shouldn't specify any cancel button here at all. Btw, it seems that a destructive button set in that method also won't work very well.
Instead of it you should:
provide a custom cancel button as the last button in the buttons array, eg: ["Action 1", "Action 2", "Close"] or sheet.addButtonWithTitle("Close")
manually set the cancel button index, e.g. sheet.cancelButtonIndex = 2
Then everything will work as expected. On the iPad the button will be automatically hidden and on the iPhone it will be styled and placed in the proper way.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
if ([UIDevice currentDevice].systemVersion.floatValue < 8.0f) {
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(8, 88, actionSheet.frame.size.width - 16, 0.5)];
separator.backgroundColor = [UIColor colorWithRed:219.0f/255 green:219.0f/255 blue:223.0f/255 alpha:1];
[actionSheet addSubview:separator];
}
}
Every button has height 44. My actionSheet doesn't have title. And I wanted to add separator between second and third buttons. That's why I use number 88.

How to validate and restrict input to UITextField inside UIAlertView in iOS

I am using the UIAlertView to show the text field over a popup. But the problem I am facing is that I am not able to validate and restrict the entries in UITextField over a popup, i.e. I want to accept only 3 numeric values.
Here i am providing the block of code that I have implemented.
popup = [[UIAlertView alloc] initWithTitle:#"Please enter 3 numeric values"
message:#"\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
txtFld = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
[txtFld setBackgroundColor:[UIColor whiteColor]];
[txtFld setKeyboardType:UIKeyboardTypeNumberPad];
[popup addSubview:txtFld];
[popup show];
[txtFld becomeFirstResponder];
So can anyone help me out to resolve this issue? Thanks in advance.
here I can see that you have not set textfield.delegate=self , please check it in your code
It is risky to manually add subviews to UIAlertView or any other view you do not control as its hierarchy is private and subject to change. If you're targeting iOS 5 or above, you can use one of the UIAlertView styles that supports text entry and validation such as UIAlertViewStylePlainTextInput. The documentation for UIAlertView provides a list of styles and this tutorial explains their use in more detail.

Show an image in the alert body of a local notification

I am using Local Notifications, but I want to put an image in the message body of the alert. How can I do that?
I also searched for the same question. And found that we can't customize the UILocalNotification, so I handled this in application:didReceiveLocalNotification: by showing custom UIAlertView.
This should work. Give it a whirl:
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)];
UIImage *smiley= [UIImage imageNamed:#"smiley.png"];
[imageView setImage:smiley];
[smiley release];
[successAlert addSubview:imageView];
[imageView release];
[successAlert show];
[successAlert release];
Good luck,
Aurum Aquila
If you are using simple images like question mark, notification, etc. I suggest Ext JS which has a Icon type alert.
You can add your images as well.

Resources