iOS determine UIAlertView size - ios

I would like to determine width of UIAlertView before I show it and after I setup it. I need that to put UIImageView into it and adjust its width to width of UIAlertView. Is there any way to do that?

It is not a very good idea to try modifying the UIAlertView's height/width or add a UIImageView to it as this might result in your app getting rejected by the App Store. However, ios-custom-alertview is a good alternative.
A sample code would be:
CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];
[alertView setContainerView:yourSubView];
[alertView setButtonTitles:[NSMutableArray arrayWithObjects:#"Close", nil]];
[alertView show];

You can use this, ios-custom-alertview
https://github.com/wimagguc/ios-custom-alertview

Related

UIAlertView has scrollable title and message on iOS7

I have a UIAlertView with text in it that can be displayed on iOS6 without any problem.
However in iOS7 the title and message of this alert are in a scrollview.
I'm creating the alert with following code
self.newCategoryAlertView = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"MessageTitleNewCategory", nil)
message:NSLocalizedString(#"MessageTextNewCategory", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(#"ButtonCancel", nil)
otherButtonTitles:NSLocalizedString(#"ButtonOK", nil), nil] autorelease];
self.newCategoryAlertView.tag = alertViewTypeNewCategory;
self.newCategoryAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.newCategoryAlertView textFieldAtIndex:0].delegate = self;
[self.newCategoryAlertView textFieldAtIndex:0].autocapitalizationType = UITextAutocapitalizationTypeSentences;
[[self.newCategoryAlertView textFieldAtIndex:0] setReturnKeyType:UIReturnKeyDone];
[[self.newCategoryAlertView textFieldAtIndex:0] setKeyboardAppearance:UIKeyboardAppearanceDefault];
[self.newCategoryAlertView textFieldAtIndex:0].enablesReturnKeyAutomatically = YES;
[self.newCategoryAlertView show];
The result on iOS7 is this:
I tried to make the the text shorter with no success and the documentation of UIAlertView didn't give me a hint.
How do I workaround this behavior?
EDIT:
This is how it looks when scrolled down. As you can see the complete message is visible.
In my opinion that's a good behaviour, as there's no space in the screen to show the keyboard and also de UIAlert. I'm almost sure that if you make the app work in portrait orientation, the alert will show ok with no scrollable title and message.
If the app only runs in landscape orientation, and you don't want to have a scrollable view, I guess the best solution will be maybe to keep the UIAlert message, but taking away the UIAlert tilte.
I hope this helped!
I have used a custom control URBAlertView. I found a similar problem with this & I have reported that to the author on the github here.
There was a fix provided by the author under the commit here. However you can also refer the top three commits on the repo here.
I hope that may help you all to figure out a solution

UIImageView disappear in iOS 7 but presented well in iOS 6

So strange since some of the photos not displayed in iOS 7 but displayed well in iOS 6.
I've both normal & #2x & -568#2x.
It happens with IBOutlet UIImageView and also when I subclass UIAlertView.
For example:
EDIT:
alert = [[UIAlertTrapAlarm alloc] initWithCameraType:camType andFrame:[UIScreen mainScreen].bounds];
[[NSNotificationCenter defaultCenter] postNotificationName:CreateDistance object:nil userInfo:dictionary];
[alert addButtonWithTitle:NSLocalizedString(#"not_exists", nil)];
[alert addButtonWithTitle:NSLocalizedString(#"exists", nil)];
[alert show];
[alert changeMessageBox:distance description:trapDesc type:trapTypeString];
[alert showHideExistsNotExistsButtons:camType];
[alert showHideButtons];
// alert.imageViewBackground.image = [UIImage imageNamed:#"allert1"];
[alert.imageViewBackground setImage:[UIImage imageNamed:#"allert1.png"]];
[alert setNeedsDisplay];
[alert.screenView setNeedsDisplay];
I forgot to mention all subviews in my subclass UIAlertView not showing.
Well you can check your Interface builder if you use Xcode 5 , there is option for viewing your Xib as 7.0 to later or 6.0 to earlier like this. See if your UIImageView is at correct place when you see it as iOS 7.0 to later. If you can't see your UIImageView then my answer might help.
I have had this problem few days ago , when I switch between iOS 6 to iOS 7 then some views became invisible and Interface builder showed garbage values at attribute inspector , like position (-680 , 675 ) , W/H (0,0) something like this. I solved this problem by unchecking AutoResizing masks of right and bottom. That solved my issue.
You can try if your problem is something similar. Because without more detail information about your codes and others , it's hard to give you a exact solution.
From that one line of code my guess is that your setting the background image in code while you set the foreground image in Interface Builder.
Try using
alert.image = [UIImage imageNamed:#"allert1"];

UIAlertView addSubview in iOS7

Adding some controls to UIAlertView was deprecated in iOS7 using addSubview method. As I know Apple promised to add contentView property.
iOS 7 is released now and I see that this property is not added. That is why I search for some custom solution with ability to add progress bar to this alertView. Something for example similar to TSAlertView, but more ready for using in iOS 7.
Here is a project on Github to add any UIView to an UIAlertView-looking dialog on iOS7.
(Copied from this StackOverflow thread.)
It took me only 1 day to create my own alert view that looks exactly like Apple's
Take a screenshot of Apple's alert for reference (font sizes, spacings, width)
Create a xib with title, message, custom view and tables for buttons (Apple uses tables instead of UIButton now, default table cell is good enough). Note you need 3 button tables: two for left and right buttons (whenever the number of buttons is 2), another one for the other cases (one button or more than 2 buttons).
Implement all the methods from UIAlertView on your custom alert.
Show/Dismiss - you can create a specific modal window for your alerts but I just put my alerts on top of my root view controller. Register your visible alerts to a static array. If showing the first alert/dismissing the last, change tint mode of your window/view controller to dimmed/to automatic and add/remove a dimming view (black with alpha = 0.2).
Blurred background - use Apple's sample code (I used opaque white)
3D dynamic effects - use Apple's sample code (5 lines of code). If you want a nice effect, take a slightly bigger snapshot in step 5 and add inverse animators for alert background and foreground.
EDIT:
Both blurred background and the paralax effect sample code can be found in "iOS_RunningWithASnap" WWDC 2013 sample code
Paralax effect:
UIInterpolatingMotionEffect* xAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis] autorelease];
xAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
xAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];
UIInterpolatingMotionEffect* yAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis] autorelease];
yAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
yAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];
UIMotionEffectGroup *group = [[[UIMotionEffectGroup alloc] init] autorelease];
group.motionEffects = #[xAxis, yAxis];
[self addMotionEffect:group];
The blurred background is the only complicated thing. If you can use an opaque color instead, use it. Otherwise it's a lot of experimenting. Also note that blurred background is not a good solution when the background is dark.
For the show/dismiss animationg, I am using the new spring animation method:
void (^animations)() = ^{
self.alpha = 1.0f;
self.transform = CGAffineTransformIdentity;
};
self.alpha = 0.0f;
self.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
[UIView animateWithDuration:0.3
delay:0.0
usingSpringWithDamping:0.7f
initialSpringVelocity:0.0f
options:UIViewAnimationOptionCurveLinear
animations:animations
completion:^(BOOL completed) {
//calling UIAlertViewDelegate method
}];
I wrote a full implementation of UIAlertView that mimics the complete UIAlertView API, but adds the contentView property we've all wanted for so long: SDCAlertView.
(source: github.io)
For those who love simple and effective methods with out having to write lines of code. Here is a cool solution without using any other private frame works for adding subviews to ios 7 alert views,i.e.
[alertView setValue:imageView forKey:#"accessoryView"];
Sample code for better understanding,
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 85, 50)];
UIImage *wonImage = [UIImage imageNamed:#"image.png"];
[imageView setImage:wonImage];
//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alertView setValue:imageView forKey:#"accessoryView"];
}else{
[alertView addSubview:imageView];
}
Hope it helps some one,thanks :)
For IOS7
UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:#"Enter Form Name"
message:#""
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView1.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *myTextField = [alertView1 textFieldAtIndex:0];
[alertView1 setTag:555];
myTextField.keyboardType=UIKeyboardTypeAlphabet;
[alertView1 show];
There wont be UIAlertView with custom views in iOS7, nor contentView which Apple changed its mind about, so addSubview is impossible now in UIAlertView.
A good alternative will be SVProgressHUD, according to many threads in Apple's forum.
Edit:
There is officially no addSubview nor subclassing for UIAlertView in iOS7.
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.
Other good alternatives:
ios-custom-alertview by wimagguc
MZFormSheetController.
You can find simple solution without extra classes here
It is based on setting accessoryView for ordinary UIAlertView.
PKAlertController (https://github.com/goodpatch/PKAlertController) is great library. I tested a lot of similar libraries and just this satisfied all my requirements.
Why it is cool:
Supports custom view
Supports iOS7
It is view controller
It behaves and looks like native alert view, including motion effects
Customizable
Similar interface like in UIAlertController

Can i put multiple text boxes in an alert box in ios?

can I put multiple text input boxes on the alert box in iOS?
The best solution is to write or use a custom alert view that has the text fields that you need. If you only need 1 text field (or username/password) you can still use UIAlertView by setting the alertViewStyle property.
There are custom alert views already written, such as this one.
Yes, that's possible.
Just add UITextField as subviews to the UIAlertView.
Set the tag for each UITextField, in order to retrieve the entered text later.
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
textField1.tag=0;
textField1.borderStyle=UITextBorderStyleRoundedRect;
textField1.delegate=self;
[alert addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)];
textField2.tag=1;
textField2.delegate=self;
textField2.borderStyle=UITextBorderStyleRoundedRect;
[alert addSubview:textField2];
[alert show];
PLEASE NOTE that this is bad practice as it relies on UIAlertView being a subclass of UIView. As pointed out by MusiGenesis below, it won't work from iOS 7 onwards.
Hope that helps.
if you want 2 text boxes, you can use login style, but set second item secureTextEntry to NO, refer to
http://forums.macrumors.com/threads/uialertviews-with-textfields.1355954/

ActionSheet not showing properly

I have this code to get NSStrings from an NSDictionary from an NSMutableArray but the problem is that it does not show right.
Here is the code:
UIActionSheet *playingSheet = [[UIActionSheet alloc] initWithTitle:#"Hi" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for(NSDictionary *dict in accounts) {
NSString *name = [dict objectForKey:#"Name"];
[playingSheet addButtonWithTitle:name];
}
[playingSheet showInView:self.view];
[playingSheet release];
The problem is in my case, self.view is a subview of a parentView and is loaded from an XIB and is not full screen. So the problem is my action sheet is like half the width of the iPhone's screen and it is not even touching the bottom of the screen.
In my case, how would I fix it so its the top most view (so users can see it properly), and the way a regular actionSheet is supposed to look?
Thanks!
Edit: This was the fix:
[playingSheet showInView:[UIApplication sharedApplication].keyWindow];
You need to set the frame of the action sheet as
[playingSheet setFrame:CGRectMake(0, theHeightYouWant, 320, yourDesiredHeight)];
"theHeightYouWant" is the height by which you must shift the action-sheet upwards/downwards to put it in the right position on the screen. You can change this by trial and error. For example, if your self.view is in the lower half of the screen, "theHeightYouWant" will be negative!
Depending how your code is set up, you could try using self.parentViewController.view, self.view.window, self.view.superview, or any of a variety of things. You need to provide more information if you want a more detailed answer.

Resources