I have a UIView with another UIView inside it. On the inside UIView there is a textbox which I want to fill in. When I try to fill it in the keyboard blocks my view:
The UIViewController has the following
containerView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view=containerView;
//The apropriate releases etc are further on...
When I touch it, the keyboard comes up as expected, but blocks the textbox I'm trying to fill in. How can I force the view to slide up?
The front view
OptionsFront * fv = [[OptionsFront alloc] initWithFrame:[UIScreen mainScreen].bounds];
[containerView addSubview:frontView];
In the front view is a subview
CGRect bounds = CGRectMake(0.0f, 210.0f, 280.0f, 130.0f);
sv = [[UIView alloc] initWithFrame:bounds];
[self addSubview:sv]; //added to frontView
In sv is a textbox near the botton:
rect = CGRectMake(70.0f, 20.0f, 100.0f, 27.0f);
cf = [self createTextField_Rounded:rect holder:#"+ve"];
[sv addSubview:cf];
So cf happens to be near the bottom of the page. I expected that when I select it, the whole display would move up, but the keyboard just moves up and blocks it.
What can I do?
Appendix:
- (UITextField *)createTextField_Rounded:(CGRect) frame holder:(NSString *) ph
{
UITextField *returnTextField = [[UITextField alloc] initWithFrame:frame];
returnTextField.borderStyle = UITextBorderStyleRoundedRect;
returnTextField.textColor = [UIColor blackColor];
returnTextField.font = [UIFont systemFontOfSize:17.0];
returnTextField.delegate = self;
returnTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
returnTextField.returnKeyType = UIReturnKeyDone;
returnTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
return returnTextField;
}
you need to move the textField up. in your UITextField delegate implement something like (from memory):
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
textField.superview.frame = CGRectMake(0.0f, 100.0f, 280.0f, 130.0f); // experiment with frame size
[UIView commitAnimations];
}
and you'll want to move it back in textFieldDidEndEditing:
Related
I am creating a app that display subview like ActionSheetView and in that i have added UIGridView and UIPageControll thru outlet , but now when i click the cell it does not respond ,my subview is a ViewController with the nib file.
and if i create a button and add in my popupview programmatically then it wil displaying, and also get the click event, but anything in outlet does not respond.
i have tried to set userinteraction enable in my subview but it does not done any good.
here is my code for first view that display subview by clicking barbutton:
- (void)popUpView:(id)sender {
//self.view.backgroundColor = [UIColor darkGrayColor];
bGView = [[UIView alloc] init]; /this is the background view and all view is added in it
bGView.frame = CGRectMake(0,0,320,490);
bGView.backgroundColor = [UIColor clearColor];
bGView.alpha = 0.8 ;
CGRect viewFrame = CGRectMake(10, 90, 300, 300);
aPopUpViewController = [[PopUpViewController alloc] initWithNibName:#"PopUpViewController" bundle:nil]; //this is my view controller and when this shows on screen it display my greed view
aPopUpViewController.view.frame = viewFrame;
aPopUpViewController.view.userInteractionEnabled = YES;
[bGView addSubview:aPopUpViewController.view];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.backgroundColor = [UIColor clearColor];
[button setBackgroundImage:[UIImage imageNamed:#"close_button.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(-1, 80, 30, 30);
[bGView addSubview:button];
//for view animation
/*
// from bottom to center animation
[bGView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen!
[UIView beginAnimations:#"animateTableView" context:nil];
[UIView setAnimationDuration:0.4];
[bGView setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen!
[UIView commitAnimations];*/
//for page like effect
/*[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionCurlUp //change to whatever animation you like
animations:^ { [self.view addSubview:bGView]; }
completion:nil];*/
/*CATransition *transition = [CATransition animation];
transition.duration = 1.0;
transition.type = kCATransitionReveal; //choose your animation
[bGView.layer addAnimation:transition forKey:nil];
[self.view addSubview:bGView];*/
aPopUpViewController.view.userInteractionEnabled = YES;
bGView.userInteractionEnabled = YES;
self.view.userInteractionEnabled = YES;
bGView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
bGView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// do something once the animation finishes, put it here
}];
[self.view addSubview:bGView];
addLabel.enabled = NO;
clipArt.enabled = NO;
emailItem.enabled = NO;
closeButton.enabled = NO;
self.navigationController.topViewController.title = #"Choose ClipArt";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
theDataObject.navBarHide = #"no";
}
- (void)aMethod:(id)sender
{
//[aPopUpViewController.view removeFromSuperview];
//[button removeFromSuperview];
/*[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve//change to whatever animation you like
animations:^ { [bGView removeFromSuperview]; }
completion:nil];*/
[bGView removeFromSuperview];
addLabel.enabled = YES;
clipArt.enabled = YES;
emailItem.enabled = YES;
closeButton.enabled = YES;
self.navigationController.topViewController.title = #"Greeting's";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
theDataObject.navBarHide = #"yes";
}
and this is how my screen look like
UPDATE
now i figured out my problem,problem is that i have given my whole view a tap listner,now i am adding grid view as subview so when ever i tap on cell rather then do select cell it will generate view tap gesture and thats why cell never selected,that's what i think..
add this line after you add every subviews in your main view..
[self.view bringSubviewToFront:yourGridView];
I have UITableViewController. In this controller I add a subview UIView with UITextField in it. When the UITextField get first responder got
setting the first responder view of the table but we don't know its
type (cell/header/footer)
Code as below:
#interface UserAlbumListViewController (){
NSString *newAlbumName;
UITextField *albumNameField;
}
#end
-(void)addAlbumButtonPressed:(id)sender{
self.tableView.scrollEnabled = NO;
CGRect frame = self.view.frame;
UIView *opaqueView = [[UIView alloc] initWithFrame:frame];
opaqueView.tag = 1001;
opaqueView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
UIView *controllerView = [[UIView alloc] init];
controllerView.tag = 1002;
controllerView.backgroundColor = [UIColor whiteColor];
controllerView.frame = CGRectMake(10.0f, 60.0f, frame.size.width - 20.0f, 90.0f);
[opaqueView addSubview:controllerView];
albumNameField = [[UITextField alloc] init];
albumNameField.borderStyle = UITextBorderStyleRoundedRect;
albumNameField.placeholder = NSLocalizedString(#"Album Name",nil);
albumNameField.keyboardType = UIKeyboardTypeDefault;
albumNameField.returnKeyType = UIReturnKeyDefault;
albumNameField.autocorrectionType = UITextAutocorrectionTypeNo;
albumNameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
albumNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
albumNameField.clearsOnBeginEditing = NO;
albumNameField.text = #"";
albumNameField.frame = CGRectMake(10.0f , 10.0f, controllerView.frame.size.width - 20.0f, 30.0f);
[albumNameField becomeFirstResponder];
[controllerView addSubview:albumNameField];
UIButton *_cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_cancelButton.backgroundColor = [UIColor clearColor];
[_cancelButton setTitle:NSLocalizedString(#"Cancel",nil) forState:UIControlStateNormal];
_cancelButton.frame = CGRectMake( controllerView.frame.size.width - 90.0f, albumNameField.frame.origin.y + albumNameField.frame.size.height + 10.0f, 80.0f, 30.0f);
[_cancelButton addTarget:self action:#selector(opaqueViewCancelButtonClicked:) forControlEvents:UIControlEventTouchDown];
[controllerView addSubview:_cancelButton];
UIButton *_OKButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_OKButton.backgroundColor = [UIColor clearColor];
[_OKButton setTitle:NSLocalizedString(#"OK",nil) forState:UIControlStateNormal];
_OKButton.frame = CGRectMake( _cancelButton.frame.origin.x - 90.f, _cancelButton.frame.origin.y, 80.0f, 30.0f);
[_OKButton addTarget:self action:#selector(opaqueViewOKButtonClicked:) forControlEvents:UIControlEventTouchDown];
[controllerView addSubview:_OKButton];
[self.view addSubview:opaqueView];
[controllerView release];
[opaqueView release];
}
How to avoid this warning?
The warning is telling you that you added opaqueView to the table view directly. Table views expect interactive subviews to be added to tableHeaderView, tableFooterView, or within a cell.
The offending line is [self.view addSubview:opaqueView]; because self.view is the table view.
You can fix this is one of several ways. If you are using a navigation controller, you can push a new controller with opaqueView in it. You can pop up a modal view over the table view with opaqueView in it. You can change the structure such that self.view is not a table view (which is a lot of work).
UPDATE
This answer is kind of busy, so I will post an update. I discovered that Apple uses a different solution then the ones I suggested.
Instead of [self.view addSubview:opaqueView]; which tries to add the subview to a table view, you can do [self.view.window addSubview:opaqueView]; which adds the subview directly to the table view's window. You need to keep in mind that you are using a view so the bounds and frame may be different than self.view.
I got this "(cell/header/footer)" error when a view pushed to the NavigationController was an instance of UITableView instead of UIView. Then in the the Xib editor for the class, I added a view to the canvas, and then text was ultimately added to the view through a couple more views down. The UI Builder will let you specify UITableView as the superclass of the view and place a view on the canvas, even though adding items to this view then caused the message "setting the first responder view of the table but we don't know its type (cell/header/footer)".
I made a UIView subclass (bowl) and when I try to make a bowl within a bowl and then center the inner one on the outer one's center it end up getting placed below and to the right of the center. How would I properly center it?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
self.userInteractionEnabled = YES;
UIGestureRecognizer *twoFingerZ = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchZoom:)] ;
[self addGestureRecognizer:twoFingerZ];
UILabel *label = [[UILabel alloc] init];
[label setFont:[UIFont fontWithName:#"Arial" size:64]];
label.text = [NSString stringWithFormat:#"March"];
[label sizeToFit];
label.center = self.center;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor lightGrayColor];
self.backgroundColor = [UIColor whiteColor];
[self addSubview:label];
}
return self;
}
-(void)pinchZoom:(UIPinchGestureRecognizer *)recognizer
{
if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateEnded) {
CGAffineTransform newForm = CGAffineTransformMakeScale(600/self.bounds.size.width, 600/self.bounds.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[self setTransform:newForm];
return;
[UIView commitAnimations];
}
if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateBegan) {
subClip = [[clipView alloc] initWithFrame:CGRectMake((self.center.x - ([delegate getSize:self].width/2)), (self.center.y - ([delegate getSize:self].height/2)), [delegate getSize:self].width, [delegate getSize:self].height)];
subClip.transform = CGAffineTransformMakeScale(0.1, 0.1);
[self addSubview:subClip];
bowl *newB = [[bowl alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
newB.center = self.center;
// right here! *************
[self addSubview:newB];
}
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
EDIT: I fixed it. Apparently the center value for a UIView isn't exactly what I thought it was (it looks like it's the position of the center in another view or something), so instead of newB.center = self.center, the right way to do it was newB.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
To center the inner label you should be able to use:
label.center=CGPointMake(self.center.x,self.center.y);
But first you'll probably want to define the frame size of the inner label:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,yourWidth,yourHeight);
You may also want to center the text within the inner label:
label.textAlignment = UITextAlignmentCenter;
Also, not sure what you're intending in your code with this:
[label sizeToFit];
But according to the documentation, sizeToFit will resize and moves the receiver view so it encloses its subviews. In this case, it doesn't appear that your inner view has subviews. So your centering may be off because you may have unexpected dimensions for your inner label. Try setting the background color for that label to something you can see (i.e. not [UIColor clearColor]) to double check its dimensions. May help solve the centering problem.
Good luck.
I noticed that Expedia app ( http://a5.mzstatic.com/us/r1000/027/Purple/4c/f4/6c/mzl.wcipbyyg.320x480-75.jpg) has a customized sectionIndex for UITableView on hotels list. (image attached)
I'm figuring that it could be a custom view, maybe with a vertical UISlider but, as I know, UISlider cannot recognize touch events on slides but just dragging ones.
So, how they do that ? A vertical sequence of buttons ? But, in this case, how to recognize dragging events ?
I would like to replicate that control but I need some hints ;)
thanks
There are more ways how to achieve the effect. Assuming you have a self.view initialized from a nib file and it's ordinary UIView, you can put this code into viewDidLoad:
- (void)viewDidLoad
{
CGRect tvFrame = self.view.frame;
tvFrame.origin = CGPointZero;
UITableView* tv = [[UITableView alloc] initWithFrame:self.view.frame];
tv.delegate = self;
tv.dataSource = self;
tv.showsVerticalScrollIndicator = NO;
[self.view addSubview:tv];
CGRect svFrame = CGRectMake(290, 10, 30, 400);
UIScrollView* sv = [[UIScrollView alloc] initWithFrame:svFrame];
sv.contentSize = CGSizeMake(10, 780);
sv.showsVerticalScrollIndicator = NO;
sv.bounces = YES;
CGRect vFrame = CGRectMake(10, 380, 10, 20);
UIView* v = [[UIView alloc] initWithFrame:vFrame];
v.backgroundColor = [UIColor blueColor];
[sv addSubview:v];
[self.view addSubview:sv];
[super viewDidLoad];
}
This will create a table with a narrow scroll view with a blue rectangle over the table. Now you can scroll both scroll views (the table is also a scroll view) independently.
Then you will have to setup delegates and receive scroll events via UIScrollViewDelegate. When one of the scroll views starts dragging, you have to start setting offset of the other scroll view.
I have a resizing issue when rotating a view controller that has two views which I switch between using the flip animation.
The problem appears if i do the following steps:
Rotate the device when viewing the tableview.
Click the info button.
Rotate the device (infoView appears stretched).
Click on info button (tableview appears stretched)
It appears that the view that is not added to the superview is not resizing correctly, since it was not part of the composite views when the device was rotated.. Is there any way to get this view auto resized correctly?
Below is a code sample
- (void)viewDidLoad {
[super viewDidLoad];
//background image
backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg-app.png"]];
backgroundImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
[self.view addSubview: backgroundImageView];
[backgroundImageView release];
//infoView
aboutImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"About.png"]];
aboutImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
//tableView
self.tableView = [[[UITableView alloc ] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain] autorelease ];
//set the rowHeight once for performance reasons.
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = 75;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.autoresizesSubviews = YES;
//set the rowHeight once for performance reasons.
[self.view addSubview: tableView];
//[tableView release];
[self.view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];//for resizing on rotation
//info button
UIButton * infoDarkButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoDarkButtonType.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
infoDarkButtonType.backgroundColor = [UIColor clearColor];
[infoDarkButtonType addTarget:self action:#selector(infoAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonInfo = [[UIBarButtonItem alloc] initWithCustomView:infoDarkButtonType];
self.navigationItem.rightBarButtonItem = buttonInfo;
[infoDarkButtonType release];
self.navigationItem.rightBarButtonItem = buttonInfo;
[buttonInfo release];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
- (void)infoAction:(id)sender{
NSLog(#"Clicked on the info button");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75]; /* Sub. duration here */
UIView *superview;
if ((superview = [tableView superview])) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:superview cache:YES];
[tableView removeFromSuperview];
[superview addSubview:aboutImageView];
} else if ((superview = [aboutImageView superview])) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:superview cache:YES];
[aboutImageView removeFromSuperview];
[superview addSubview:tableView];
}
[UIView commitAnimations];
}
Thanks
Try putting the code in viewWillAppear:animated. It's called every time your view will appear. View did load is called once.