iPhone - AlertView text field shifts using iOS6 [duplicate] - ios

This question already has answers here:
UIAlertView with textfield and three buttons issue in ios 6
(4 answers)
Closed 9 years ago.
I have a problem with my UIAlertView text field using iOS6.
The text field shifts all the way to the top. When i use iOS7, the text field is in the right spot.
Can someone help me? Here is the code:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Item?"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Add another", #"Done",nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message textFieldAtIndex:0].autocapitalizationType = UITextAutocapitalizationTypeWords;
[message show];
The text field appears on the title text (here: Item?)

did you try this :
-(void)willPresentAlertView:(UIAlertView *)alertView {
if ([[[UIDevice currentDevice] systemVersion] floatValue] <7)
{
[alertView setFrame:CGRectMake(17, 30, 286, 280)];
NSArray *subviewArray = [alertView subviews];
UILabel *message = (UILabel *)[subviewArray objectAtIndex:2];
[message setFrame:CGRectMake(10, 46, 260, 20)];
UIButton *cancelbutton = (UIButton *)[subviewArray objectAtIndex:3];
[cancelbutton setFrame:CGRectMake(10, 125, 260, 42)];
UIButton *savebutton = (UIButton *)[subviewArray objectAtIndex:4];
[savebutton setFrame:CGRectMake(10, 170, 260, 42)];
UIButton *saveAddbutton = (UIButton *)[subviewArray objectAtIndex:5];
[saveAddbutton setFrame:CGRectMake(10, 220, 260, 42)];
UITextField *textfield = (UITextField *)[subviewArray objectAtIndex:6];
[textfield setFrame:CGRectMake(10, 80, 266, 50)];
UITextField *placeTF = (UITextField *)[subviewArray objectAtIndex:7];
[placeTF setFrame:CGRectMake(15, 70, 256, 50)];
}
}

Actually this can be fixed manually by detecting the system version. Here is the right way to do this in this post

Try this code, it will work.
- (IBAction)showAlert:(id)sender
{
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.delegate = self;
alertView.title = #"Item?";
alertView.message = nil;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView addButtonWithTitle:#"Cancel"];
[alertView addButtonWithTitle:#"Add another"];
[alertView addButtonWithTitle:#"Done"];
[alertView show];
}

Related

How to create UIProgressViewStyleBar on UIAlertview iOS 7?

I am trying to create UIProgressViewStyleBar on UIAlertview with JSON loading status info on my app. Now my problem is I cant see UIProgressViewStylebar on iOS 7. Please help me!
My Source:
UIAlertView *alertss = [[UIAlertView alloc] initWithTitle:#"Please Wait..Downloading reports..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] ;
alertss.frame=CGRectMake(50, 50, 280, 40);
UIProgressView *prgView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
prgView.frame = CGRectMake(5, 0, 70, 20);
prgView.hidden=NO;
[alertss addSubview:prgView];
[alertss show];
UILabel *statusLabel = [[UILabel alloc] init];
statusLabel.backgroundColor = [UIColor clearColor];
statusLabel.textColor = [UIColor whiteColor];
statusLabel.font = [UIFont fontWithName:#"AmericanTypewriter-Condensed" size:18.0];
statusLabel.frame = CGRectMake(120, 80, 80, 20);
[alertss addSubview:statusLabel];
[alertss show];
Yeah that is the correct code with that code add this line
[progressView setProgress:0.75f animated:YES];
before showing your alert. change the progress value depends on your need. or animate it if you want.
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Running" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
UIProgressView* progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progressView.frame = CGRectMake(0, 0, 200, 15);
progressView.bounds = CGRectMake(0, 0, 200, 15);
progressView.backgroundColor = [UIColor blackColor];
[progressView setUserInteractionEnabled:NO];
[progressView setTrackTintColor:[UIColor blueColor]];
[progressView setProgressTintColor:[UIColor redColor]];
[progressView setProgress:0.75f animated:YES];
// you have to set the accessoryView to alertview
[av setValue:progressView forKey:#"accessoryView"];
[av show];
see adding MBprogressHUD is so simple. first download the MBProgressHUD Lib from the GitHub and import in your project as #import "MBProgressHUD.h"
And then simple line to show the progressHUD is as follows :
to show:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
to hide:
[MBProgressHUD hideHUDForView:self.view animated:YES];
for your problem use this code:
av = [[UIAlertView alloc] initWithTitle:#"Running" message:#"" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[av show];
[MBProgressHUD hideHUDForView:self.view animated:YES];
Here I used the circular loader. if you want to customise it you can use this loader in many forms as you wish.

How can I Add TextField & CheckBox into the same AlertView

I've added CheckBox into AlertView now I'd like to add TextField too into the same AlertView.
UIAlertView *alertForExistingCredit = [[UIAlertView alloc] init];
rememberButton = [[UIButton alloc] init];
UIView *v = [[UIView alloc] init];
[rememberButton setImage:[UIImage imageNamed:#"PortraitPhoto_Checkbox-Normal.png"]
forState:UIControlStateNormal];
[rememberButton setTitle:#"Save Code" forState:UIControlStateNormal];
[rememberButton addTarget:self action:#selector(toggleRememberMethod) forControlEvents:UIControlEventTouchUpInside];
alertForExistingCredit = [[UIAlertView alloc] initWithTitle:#"PrePay Code"
message:#""
delegate:self
cancelButtonTitle:kCancelMessage
otherButtonTitles:kAlertOkMessage, nil];
[v addSubview:rememberButton];
If I use:
//plain text part
// alertForExistingCredit.alertViewStyle = UIAlertViewStylePlainTextInput;
// [[alertForExistingCredit textFieldAtIndex:0] setPlaceholder:#"PrePay Code"]; this lines of code it is not showing on the screen
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
v.frame = CGRectMake(0, 0, 250, 40);
rememberButton.frame = CGRectMake(0.0, 0, 250, 50.0);
[rememberButton setTitleColor:[UIColor blackColor]
forState:UIControlStateNormal];
alertForExistingCredit.message = #"Please insert your PrePay code";
[alertForExistingCredit setValue:v forKey:#"accessoryView"]
}else {
v.frame = CGRectMake(0, 80, 250, 40);
rememberButton.frame = CGRectMake(0.0, 0, 250, 40.0);
alertForExistingCredit.message = #"Please insert your PrePay code";
[alertForExistingCredit addSubview:v];
}
alertForExistingCredit.tag = 0;
[alertForExistingCredit show];
}
Try to add this UITextField to your v (UIView), and try to adjust frame size and location.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 250, 50)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[v addSubview:textField];
Add add a subview into your UIAlertView and put your buttons into that view, see the code to add a UIView into your UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TEST" message:#"subview" delegate:nil cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
[alert setValue:view forKey:#"accessoryView"];
// add your button in *view*.
[alert show];

Add UIActivityIndicatorView to UIActionSheet

I am trying to add an UIActivityIndicatorView to UIActionsSheet in this way:
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:sharedInstance
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for(NSMutableDictionary *dict in moreStructure)
{
NSString *value = [dict valueForKey:TITLE_PARAMETER];
[actionSheet addButtonWithTitle:value];
}
[actionSheet addButtonWithTitle:#"Cancel"];
actionSheet.cancelButtonIndex = [moreStructure count];
[actionSheet showInView:((SectionViewController *)sharedInstance.currentViewController).view];
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.frame = CGRectMake(100, 500, 15, 15);
[activityView startAnimating];
[actionSheet addSubview:activityView];
When the UIActionSheet appears, however, I do not see an UIActivityIndicatorView on it. Please, help me.
I have try this and this is working... Please check frame of activityView... because its y is 500...
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:nil
cancelButtonTitle:#"Test"
destructiveButtonTitle:#"Test"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.frame = CGRectMake(100, 5, 15, 15);
[activityView startAnimating];
[actionSheet addSubview:activityView];
activityView.backgroundColor = [UIColor redColor];
actionSheet.backgroundColor = [UIColor greenColor];
activityView.frame = CGRectMake(100, 500, 15, 15);
// this so this above must change
activityView.frame = CGRectMake(10, 50, 300, 400);

UIDatePicker Cancel Overlaps

The CANCEL area is over the top of the date, the picker still works but is hidden under the cancel button.
This code used to work so I am guessing its something with the newer OS?
I haven't needed to mess with the iPhone code for a while and now find a problem with a date picker. So I am not up to speed at all on XCode but trying to muddle through to see what the problem is. I guess I will eventually get up to speed again and figure out what is wrong, but if anyone can tell me what it is that would be awesome.
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet {
switch ([actionSheet tag] ) {
case 1://date
{
NSDate *d;
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, 100, 116)];
[pickerView setTag:100+[actionSheet tag]];
[pickerView setDatePickerMode:UIDatePickerModeDate];
[actionSheet addSubview:pickerView];
[pickerView release];
NSArray *subViews = [actionSheet subviews];
[[subViews objectAtIndex: SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
[[subViews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];
}
break;
}
}
- (IBAction)btnDate:(id)sender {
UIActionSheet *asheet = [[UIActionSheet alloc]
initWithTitle:#"Pick the date"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Select"
, nil];
[asheet setTag:1];
[asheet showInView:[self.view superview]];
[asheet setFrame:CGRectMake ( 0, 117, 320, 383)];
[asheet release];
}

UIPicker / UIActionSheet works fine on iPhone but only thin line on iPad

The following Code works fine on iPhone, but not on iPad. Only get a thin line instead of the full view. Have been researching and maybe something with the rect for the picker? All size numbers seem to be reasonable.
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil
cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
if ( isipad ) pickerFrame = CGRectMake(0, 345, 400, 216);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Select"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(10, 7.0f, 100.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];
UISegmentedControl *skipButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Skip Birth Year"]];
skipButton.momentary = YES;
skipButton.frame = CGRectMake(200, 7.0f, 100.0f, 30.0f);
skipButton.segmentedControlStyle = UISegmentedControlStyleBar;
skipButton.tintColor = [UIColor blackColor];
[skipButton addTarget:self action:#selector(skipActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:skipButton];
[skipButton release];
[actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
Well, got it to work by putting most of the code in
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
including
pickerFrame = CGRectMake(0, 0, 320, 214);
AND
initWithTitle:#"\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
for the action sheet. Magic I guess.
Is this correct:
CGRect pickerFrame = CGRectMake(0, 40, 0, 0); //This line sets the frame height and width equal to 0.
if ( isipad ) pickerFrame = CGRectMake(0, 345, 400, 216);
Is your isipad bool working ok?
Thanks for your post ort11.
I needed to make an iPad app and I did working copy-paste code from iPhone app and on iPad is the same: a small line :)
My needs is a language chooser only, nothing else.
Here are the codes based on your answer:
- (IBAction)btLanguageChooserPressed {
NSLog(#"btLanguageChooserPressed");
[self createLanguageSheet];
// show the sheet: will be invoked the willPresentActionSheet method
[sheet showInView:self.view];
}
- (void)createLanguageSheet {
NSLog(#"createLanguageSheet()");
// hoolly crap, need to initialize the title with: #"\n\n\n\n\n\n\n\n\n\n\n\n\n\n" to get working!!!
sheet = [[UIActionSheet alloc] initWithTitle:#"\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[sheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
CGRect pickerFrame = CGRectMake(0, 0, 320, 216);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[sheet addSubview:pickerView];
// add close button:
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"OK"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(120, 12, 100, 30);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissSheet:) forControlEvents:UIControlEventValueChanged];
[sheet addSubview:closeButton];
[sheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
//[sheet showInView:self.view];//already called, why need again?
//[sheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
}
at runtime I do a cache in a variable:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected: %#", [arrayLanguages objectAtIndex:row]) ;
selectedRow = row;
}
and here is the finish:
- (void)dismissSheet:(id)sender {
[sheet dismissWithClickedButtonIndex:0 animated:YES];
NSString* selectedLanguage = [arrayLanguages objectAtIndex:selectedRow];
NSLog(#"dismissSheet() selectedLanguage: %#",selectedLanguage);
switch (selectedRow) {
case 0://English
imgCurrentLanguage.image = [UIImage imageNamed:#"english.png"];
break;
case 1:// Deutsch
break;
case 2://Français
break;
case 3://Italiano
break;
case 4://Español
break;
default:
break;
}
// TODO: do the changes in GUI
}
I am not able to move this actionsheet from the center of the screen to botton- left corner. But I am trying...
I think this would be the cleanest solution :
#interface MyActionSheet : UIActionSheet
#property (nonatomic, strong) IBOutlet UIView * customView;
#end
#implementation MyActionSheet
-(CGRect)bounds {
return self.customView.bounds;
}
#end
then:
MyActionSheet * s = [[MyActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
s.customView = _auxView;
[s addSubview:_auxView];

Resources