My storyboard elements are subviews of containerView and containerView is a subview of the main view. I am trying to resize the height of my container view when an ad is available to show but I cannot get that to work. I am able to offset the view up, but I am not able to resize it. I have seen quite a bit a posts about this, but all suggestions I've read basically say to confirm Autolayout is not checked. I have confirmed that Autolayout is not checked in each element of my storyboard but still am not having success with this. The ad banner (placed just off screen at [0, 480] pops up nicely from the bottom just like I want it to, but it covers up my storyboard elements which is just plain UNACCEPTABLE. I will not stand for this! I need some help guys and gals…Please see code below:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
_containerView.frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
Try it like this:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
CGRect frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
[_containerView setFrame:frame];
CGRect frame2 = banner.frame;
frame2.origin.x = 0;
frame2.origin.y = -50;
[banner setFrame:frame2];
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
You have just changed the frame of your containerView. This does not in any way change the frames of the subviews of containerView. You either have to make containerView a scrollview or change the frames of every component.
Related
I've been following this and a bunch of other tutorials/blogs,etc trying to get my view to scroll when the user hits "return" and ends on a UITextField that is being covered by the keyboard but nothing is actually working.
I'm wondering what must I be doing or missing that is causing this?
Basically:
the user wants to do something in the app: add a credit card
show a UIView that contains all the CC fields
1/2 of the UITextFields are covered by the keyboard
scroll the view when the user gets there.
Nothing is happening. The code for detecting the 'covered by keyboard' case is being hit and executed but: [self.scrollView setContentOffset:scrollPoint animated:YES] has no effect at all.
Thoughts?
Reveal screenshot:
code: same as the tutorial in the link. Nothing new there.
Did you check the contentSize property?
To make a scroll view scrollable, the content must be larger than the display area(usually the bounds of the scrollview).
You can achieve this by explicitly setting the contentSize property which is CGSizeZero by default
In addition, setting the contentInset property to adjust the size of display area(in this case you can set bottom value equals keyboard height) when the keyboard is popped up
Try this,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y-keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
and
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y+keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
What I do to avoid the textField being covered by the keyboard is to animate the textfield to the top of the screen when the user touches it to start entering something with the keyboard and animate it back when done. It is much more elegant in my opinion. Maybe that could be an alternative solution for you... I do it for the different screensizes with different numbers... you might need to adjust it of course...
So listen to the textField delegate and in
- (void)textFieldDidBeginEditing:(UITextField *)textField {
you do
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 568){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 667){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -238; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 736){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -251; [self.view setFrame:frame]; [UIView commitAnimations];});
}
and in
- (void)textFieldDidEndEditing:(UITextField *)textField {
you just animate back
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = 0; [self.view setFrame:frame]; [UIView commitAnimations];});
I need a UIView to be hidden from the screen initially and then slides up. Kind of like a UIActionSheetView animation. In IB, I have set a UIView stick to the bottom border of the superview. In code, I have changed its frame to be hidden away from the screen when the app starts. And when it starts, the UIView slides up. Here's is my code. This optionsSheetView is the 'UIView' I'm referring to.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Hide the UIView from the screen initially
self.optionsSheetView.frame = CGRectMake(0, self.optionsSheetView.frame.origin.y + self.optionsSheetView.frame.size.height, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
// Sliding up animation
[UIView animateWithDuration:0.9 delay:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.optionsSheetView.frame = CGRectMake(0, 374, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
} completion:^(BOOL finished) {
}];
}
Now here's my problem. This works just fine. But I don't like keeping hard coded values in my code. Like 374 for the optionsSheetView's y coordinate. If I put self.optionsSheetView.frame.origin.y there, it won't work. It's basically the same thing. When I replace it with its actual value which is 374, it works fine.
Why is this happening? Is there any way I can go about this without using magic numbers?
Thank you.
Try following:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGFloat oldY = self.optionsSheetView.frame.origin.y;
// Hide the UIView from the screen initially
self.optionsSheetView.frame = CGRectMake(0, self.optionsSheetView.frame.origin.y + self.optionsSheetView.frame.size.height, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
// Sliding up animation
[UIView animateWithDuration:0.9 delay:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.optionsSheetView.frame = CGRectMake(0, oldY, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
} completion:^(BOOL finished) {
}];
}
im trying to do an animation on UIPicker where when my app runs its hidden and when the button is pressed it will appear sliding from the buttom and when i tap the button again it will slide away. well, my code does that but only just once.
it does:
first click -> show the picker, isPickerHidden = NO
second click -> hides picker, isPickerHidden = YES
third click -> does not do anything, but it returns isPickerHidden = NO
where in viewDidLoad declaration are;
and same as the fourth click it doesnt do anything but return the correct BOOL value.
isPickerHidden = YES;
[self.picker setHidden:isPickerHidden];
if(isPickerHidden == NO){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGRect frame = self.picker.frame;
[self.picker setFrame:CGRectOffset(frame, self.picker.frame.origin.x, self.picker.frame.origin.y)];
[UIView commitAnimations];
isPickerHidden = YES;
NSLog(#"hidden yes");
}else if(isPickerHidden == YES) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, -200);
self.picker.transform = transfrom;
[self.picker setHidden:NO];
[UIView commitAnimations];
NSLog(#"hidden no ");
isPickerHidden = NO;
}
i would like to know on whats the problem i am facing, and what am i missing. thanx
add some logging for the frame and picker to see what is happening...
NSLog(#"picker: %.0f, %.0f, %.0f, %.0f", self.picker.frame.origin.x, self.picker.frame.origin.y, self.picker.frame.size.width, self.picker.frame.size.height);
thanx NS bro for the reply but i did a different approach now instead of offsetting the points from the original one i used CGRectMake, i did this
if(isPickerHidden == NO) {
CGRect frame = self.picker.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.6];
[self.picker setFrame:CGRectMake(frame.origin.x, frame.origin.y + 216, frame.size.width, frame.size.height)];
[UIView commitAnimations];
isPickerHidden = YES;
} else if (isPickerHidden == YES) {
CGRect frame = self.picker.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.6];
[self.picker setFrame:CGRectMake(frame.origin.x, frame.origin.y - 216, frame.size.width, frame.size.height)];
[UIView commitAnimations];
isPickerHidden = NO;
}
both the sliding down and sliding up are doing well animated in the same manner. but now the problem is i wanted to make the UIPicker disappear on app load so in the viewDidLoad i did try to use the [self.picker setHidden:YES]; but the frames i made using CGRect dont appear when i press the button and i also tried alpha still nop.
i did also put in the body of the if-else statement the setHidden to yes and to know with respect to their values.
would like to know what im missing now.
I have developed a font selection that utilizes UIPickerView and UIToolbar, which are both added on touching a UIButton.
But they just kind of pop in which looks sloppy.
is there a way to animate them?
here is the method that adds them (it's called on clicking the button) :
-(void)showPicker
{
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
}
You can animate them in a couple of ways, but probably the easiest is to just fade them in.
Fade In
[someView setAlpha:0]
[self.view addSubview:someView];
[UIView beginAnimations:#"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:1];
[UIView commitAnimations];
Fade Out
float fadeDuration = 0.5;
[UIView beginAnimations:#"FadeOut" context:nil];
[UIView setAnimationDuration:fadeDuration ];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:0];
[UIView setAnimationDidStopSelector:#selector(removeView)];
[UIView commitAnimations];
-(void) removeView {
[someView removeFromSuperview];
}
Something as simple as that.
Depends on what type of animation you want to use. For example, this would look like dissolve.
Anyways look into UIView animateWithDuration...
pickerTB.alpha = _fontPicker.alpha = 0;
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
[UIView animateWithDuration:1 animations:^{_fontPicker.alpha = 1; pickerTB.alpha = 1}];
You could use transforms or change frame origins to make the views fly in from positions outside the screen too. In that case, inside the animations block, specify the new on screen position. You can do this in two ways, change the frame with an updated origin or apply a CGAffineTransformMakeTranslation(dx,dy)
You need to add the subview with a frame value that is where you want the animation to begin from (off-screen?) then change the frame value inside your animation block. The frame will change over the duration, causing the appearance of movement. The view must already be a subview - I don't believe adding a subview is something you can animate, it makes no sense.
-(void) showPicker{
[self.view addSubview: _fontPicker];
_fontPicker.frame = CGRectMake(0, 0, 120, 40);// somewhere offscreen, in the direction you want it to appear from
[UIView animateWithDuration:10.0
animations:^{
geopointView.frame = // its final location
}];
}
Initially set your picker view frame and tool bar frame like this one..
-(void)viewDidLoad
{
[super viewDidLoad];
pickerTB.frame = CGRectMake(0,568,320,44);
fontPicker.frame = CGRectMake(0, 568, 320, 216);
}
-(void)showPicker
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.30];
if(isiPhone5)
{
pickerTB.frame = CGRectMake(0,288,320,44);
fontPicker.frame = CGRectMake(0, 332, 320, 216);
}else{
pickerTB.frame = CGRectMake(0,200,320,44);
fontPicker.frame = CGRectMake(0,244, 320, 216);
}
[UIView commitAnimations];
}
I have a UIScrollView at the very top (just below the navigation bar) of a UITableViewController using Storyboards. The UIScrollView's default size is 320x50. When the user holds the scrollView for at least 1 second, I want the UIScrollView to get twice as big (320x100) in order to show additional details with an animation. The UITableView right below it should animate with it if possible and move down 50.
Holding the UIScrollView for at least 1 second when it's in the bigger setting will result in the opposite animation, and the UIScrollView will animate back to its original size.
How would I do this? Thank you ahead of time! Here's what I have so far:
- (void)viewDidLoad {
[super viewDidLoad];
self.featureScrollExpanded = NO;
UILongPressGestureRecognizer* featureHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector (longHold:)];
[featureHold setDelaysTouchesBegan : YES];
featureHold.minimumPressDuration = 0.6;
featureHold.numberOfTouchesRequired = 1;
[self.featureScrollView addGestureRecognizer:featureHold];
[featureHold release];
}
- (void)longHold:(UIGestureRecognizer*)sender {
if (self.featureScrollExpanded==NO) {
self.featureScrollExpanded=YES;
//make it bigger
}
else {
self.featureScrollExpanded=NO;
//make it smaller
}
}
Do an animation:
CGRect frame = yourScrollView.frame;
frame.size.height += 50;
[UIView animateWithDuration:0.6
animations:^{
yourScrollView.frame = frame;
} completion:^(BOOL finished) {
//do your other animation here if you want to
//or do them both together and lose this block
}
];
Or if you want it a bit more with an overview:
CGRect frame = yourScrollView.frame;
frame.size.height += 50;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.6];
[self.yourScrollView setFrame:frame];
[UIView commitAnimations];