I have a search button and when user clicks it I am displaying UISearchBar programmatically. Width of Searchbar is (200) not full screen. I am searching like a MSWord and displaying the content in searchbar' tableview. But when user is in middle of search and changed orientation how should I control the search bar width, i.e., self.searchDisplayController.searchBar.frame and self.searchDisplayController.searchResultsTableView.frame.
Below is the piece of code that used to create the searchbar:
-(IBAction)searchBar:(id)sender
{
if(!searching)
{
searching = YES;
sBar.frame = CGRectMake(500, 50,250, 44);
[self.view addSubview:sBar];
[searchController setActive:YES animated:YES];
[sBar becomeFirstResponder];
if((currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
(currentOrientation == UIInterfaceOrientationLandscapeRight))
{
self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44);
}
else
{
self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44);
}
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
/*
Bob: Because the searchResultsTableView will be released and allocated automatically, so each time we start to begin search, we set its delegate here.
*/
[self.searchDisplayController.searchResultsTableView setDelegate:self];
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:204/255.0 alpha:1.0]];
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
/*
Hide the search bar
*/
float animateTime = 0.3;
CGRect viewFrame = sBar.frame;
viewFrame.origin.y -= (viewFrame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animateTime];
sBar.frame = viewFrame;
[UIView commitAnimations];
[self performSelector:#selector(animationDone) withObject:nil afterDelay:0.3];
}
-(void)animationDone
{
[sBar removeFromSuperview];
searching = NO;
}
-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
if((currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
(currentOrientation == UIInterfaceOrientationLandscapeRight))
{
tableView.frame = CGRectMake(755, 100, 250, 400);
}
else
{
tableView.frame = CGRectMake(500, 100, 250, 400);
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
currentOrientation = toInterfaceOrientation;
if((currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
(currentOrientation == UIInterfaceOrientationLandscapeRight))
{
//Search
btnSearch.frame = CGRectMake(920, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44);
self.searchDisplayController.searchResultsTableView.frame = CGRectMake(755, 100, 250, 400);
}
else
{
//Search
btnSearch.frame = CGRectMake(666, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44);
self.searchDisplayController.searchResultsTableView.frame = CGRectMake(500, 100, 250, 400);
}
}
I am able to create and functionality which is working fine. But only issue is when user change the orinentation its not properly setting searchbar' width.
My application needs to be delivered this friday. Please answere me asap.
Answering my question to close this thread as well to help others.
(1) In willRotateToInterfaceOrientation its able to set UISearchBar origin. Width is coming as 768 (full screen width of iPad in Portrait) but origin seems to be set here.
(2) After orientation change finished we can set the search bar width. With small animation it will be smooth resize.
(3) I am calling resetFrame at both pre & post orientation APIs to avoid some sudden JURK kind for searchBar.
Below is the solution i used for my app..
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
currentOrientation = toInterfaceOrientation;
[self resetSearchResourcesFrames]; // Set the origin of UISearchBar
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[UIView beginAnimations:#"" context:nil]; //Animation for smooth resizing of search tableview only
[UIView setAnimationDuration:0.2];
[self resetSearchResourcesFrames]; // Width rotation only possible after rotation
[UIView commitAnimations];
}
-(void)resetSearchResourcesFrames
{
if(([buttonGridViewController sharedInstance].currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
([buttonGridViewController sharedInstance].currentOrientation == UIInterfaceOrientationLandscapeRight))
{
//Search
btnSearch.frame = CGRectMake(920, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44);
self.searchController.searchResultsTableView.frame = CGRectMake(755, 100, 250, 400);
}
else
{
//Search
btnSearch.frame = CGRectMake(666, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44);
self.searchController.searchResultsTableView.frame = CGRectMake(500, 100, 250, 400);
}
}
Related
I set frame for all UI elements in ViewDidload and changing their frames in
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
The problem is when I try to Navigate from View A --> View B in portrait and then change orientation landscape comeback to View A <-- View B. Then frames are not chaning accordingly.
I tried adding
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
But still frames are not changing even viewWillTransitionToSize is not getting called when it comebacks.
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [UIScrollView new];
scrollView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-hSelecViewHeight);
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.bounces = true;
scrollView.alwaysBounceVertical = true;
[self.view addSubview:scrollView];
imagePager = [[KIImagePager alloc]init];
imagePager.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.563);
imagePager.delegate = self;
imagePager.dataSource = self;
imagePager.slideshowTimeInterval = 2.0;
[imagePager setImageCounterDisabled:true];
[scrollView addSubview:imagePager];
-- and few other UI elements also --
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.barTintColor = barColor;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
hSelView.frame = CGRectMake(0,self.view.frame.size.height-hSelecViewHeight,self.view.frame.size.width,hSelecViewHeight);
scrollView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-hSelecViewHeight);
imagePager.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.563);
[hSelView reloadData];
if (categoryIndex!=0){
tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, (259*[[arrListData valueForKey:#"sub_category_name"] count])+100);
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, ([[arrListData valueForKey:#"sub_category_name"] count]*259)+100)];
}else{
tableView.frame = CGRectMake(0, imagePager.frame.origin.y+imagePager.frame.size.height+1, self.view.frame.size.width, (259*[[arrList valueForKey:#"name"]count])+100);
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, ([[arrList valueForKey:#"name"]count]*259)+(self.view.frame.size.width*0.563-40)+100)];
}
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
NSLog(#"orientation:- %ld",(long)orientation);
if (!isMaximized) {
content.view.frame = CGRectMake(self.view.frame.size.width-(self.view.frame.size.width/1.5)-10, self.view.frame.size.height-hSelecViewHeight-10-((self.view.frame.size.width/1.5)*0.563), self.view.frame.size.width/1.5, (self.view.frame.size.width/1.5)*0.563);
closePopButton.frame = CGRectMake(content.view.frame.size.width-40, 5, 36, 36);
hSelView.frame = CGRectMake(0,self.view.frame.size.height-hSelecViewHeight,self.view.frame.size.width,hSelecViewHeight);
scrollView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-hSelecViewHeight);
imagePager.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.563);
[hSelView reloadData];
if (categoryIndex!=0){
tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, (259*[[arrListData valueForKey:#"sub_category_name"] count])+100);
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, ([[arrListData valueForKey:#"sub_category_name"] count]*259)+100)];
}else{
tableView.frame = CGRectMake(0, imagePager.frame.origin.y+imagePager.frame.size.height+1, self.view.frame.size.width, (259*[[arrList valueForKey:#"name"]count])+100);
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, ([[arrList valueForKey:#"name"]count]*259)+(self.view.frame.size.width*0.563-40)+100)];
}
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
Please add for updated code in viewWillAppear method.
- (void)viewWillAppear:(BOOL)animated
{
}
Call viewWillTransitionToSize method in viewWillAppear
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self viewWillTransitionToSize:self.view.frame.size withTransitionCoordinator:[self transitionCoordinator]];
}
I have 13 text fields. I want to set view animation for only 4 text fields which will be displayed in bottom
Here is my code:
if (textField.editing != ((_GET_companyname)||(_GET_firstname)||(_GET_middlename)||(_GET_lastname)||(_GET_companyurl)||(_GET_streetaddress)||(_GET_postbox)||(_GET_code)||(_GET_phonenum)))
{
NSLog(#"Entered in condition");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//Keyboard becomes visible
[UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.25];
self.view.frame = CGRectMake(0,-200,self.view.frame.size.width,self.view.frame.size.height);
[UIView commitAnimations];
}
}
else
{
NSLog(#"Entered else Part");
}
I need to set the below to all 14 textfields
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
According to you if "_GET_companyname" is a IBOutlet of UITextField.
Then you should write something like this below
if ((textField != _GET_companyname)&&(textField != _GET_firstname)) // etc
{
NSLog(#"Entered in condition");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//Keyboard becomes visible
[UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.25];
self.view.frame = CGRectMake(0,-200,self.view.frame.size.width,self.view.frame.size.height);
[UIView commitAnimations];
}
}
else
{
NSLog(#"Entered else Part");
}
Edit :- And also instead of checking (!=). you should check (==) for few UITextFields. That will be better.
You can check these using tags
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textFiledName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
textFiledName.tag = 0;
textFiledName.delegate = self;
[self.view addSubview:textFiledName];
UITextField *textFiledLastName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
textFiledLastName.tag = 1;
textFiledLastName.delegate = self;
[self.view addSubview:textFiledLastName];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField.tag == 0) {
//No Animation
}else if (textField.tag == 1){
//Add Animation
}
return YES;
}
I’m working on a UI effect, with “pull up to load more” and “pull down to go back”.
It should works like: When user pull theUIScrollView up by a certain distance, a load-more action is triggered, and lower half of theUIScrollView’s content would be scrolled into visible area. After that, if user pull down thescrollView by a distance, it should roll back to the upper half.
I have implemented it by settingcontentInsets of theUIScrollView when dragging triggered the action.
The problem is that at the time of changing of thescrollView’s contentInsets, it flickers. I printed some logs and found that it is caused by an unusually jump ofcontentOffset.y whencontentInsets changes! But I don’t know why.
The following are simplified but enoughViewController codes for reproducing the issue. With logs you can see clearly what happens.
Could someone helps me to solve it?
#import "IssueShowcaseViewController.h"
#interface IssueShowcaseViewController () <UIScrollViewDelegate>
{
BOOL _isShowingUpperView;
}
#property (nonatomic, strong) UIScrollView* scrollView;
#property (nonatomic, strong) UIView* upperView;
#property (nonatomic, strong) UIView* lowerView;
#end
#implementation IssueShowcaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.upperView = [[UIView alloc] initWithFrame:self.scrollView.bounds];
self.lowerView = [[UIView alloc] initWithFrame:CGRectOffset(self.scrollView.bounds, 0, self.scrollView.bounds.size.height)];
[self.upperView setBackgroundColor:[UIColor blueColor]];
[self.lowerView setBackgroundColor:[UIColor greenColor]];
//Add upperView to upper half, and lowerView to lower half:
[self.scrollView addSubview:self.upperView];
[self.scrollView addSubview:self.lowerView];
[self.view addSubview:self.scrollView];
_isShowingUpperView = YES;
self.scrollView.delegate = self;
// Observe scrollView.contentOffset for watching unnormal changes:
[self.scrollView addObserver:self forKeyPath:#"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
// Set contentSize as the sum height of two subviews:
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(self.lowerView.frame) - CGRectGetMinY(self.upperView.frame));
// But the contentInset is set as to not allow scrolling into lower half:
// 1-pixel more space is necessary, as the scrollView would not be able to scroll downwards without it:
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 1-self.lowerView.frame.size.height, 0);
}
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (_isShowingUpperView)
{
if (scrollView.contentOffset.y > 60)
{
scrollView.contentInset = UIEdgeInsetsMake(1-self.upperView.frame.size.height, 0, 0, 0);
_isShowingUpperView = NO;
}
}
else
{
if (scrollView.contentOffset.y < self.upperView.frame.size.height - 60)
{
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 1-self.lowerView.frame.size.height, 0);
_isShowingUpperView = YES;
}
}
}
#pragma mark Here we can see what happens:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"contentOffsetY = %f", scrollView.contentOffset.y);
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self.scrollView) {
CGPoint oldOffset = [[change objectForKey:#"old"] CGPointValue];
CGPoint newOffset = [[change objectForKey:#"new"] CGPointValue];
if (fabs(newOffset.y - oldOffset.y) > 300) {
NSLog(#"Weird: %#", change);
}
}
}
#end
After so many tries, I have found a solution: First set a zero contentInsets in an animation block, then in the completion block, set the new contentInsets and correct contentOffset:
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (_isShowingUpperView)
{
if (scrollView.contentOffset.y > 60)
{
[UIView animateWithDuration:0.0f animations:^{
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3f animations:^{
scrollView.contentOffset = CGPointMake(0, self.upperView.frame.size.height);
} completion:^(BOOL finished) {
scrollView.contentInset = UIEdgeInsetsMake(1-self.upperView.frame.size.height, 0, 0, 0);
}];
}];
_isShowingUpperView = NO;
}
}
else
{
if (scrollView.contentOffset.y < self.upperView.frame.size.height - 60)
{
[UIView animateWithDuration:0.0f animations:^{
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3f animations:^{
scrollView.contentOffset = CGPointMake(0, 0);
} completion:^(BOOL finished) {
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 1-self.lowerView.frame.size.height, 0);
}];
}];
_isShowingUpperView = YES;
}
}
}
I have a UITableViewController that I want to add iAds to. I want the ad to display at the bottom of the screen at all times. I have followed the answer here: https://stackoverflow.com/a/9857798/2584268 and have achieved this behavior. However, the ad is hidden in the beginning and only appears when the user scrolls the table. How can I get the ad to appear at the bottom of the screen when the view loads, not just when the user scrolls?
To show the ad when the view appears, I add the banner view on viewDidAppear.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_myIAD = [[self appdelegate] myIAD];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//iPhone code
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 50, 320, 50);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 114, 320, 50);
}
}
else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 32, 480, 32);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 84, 480, 32);
}
}
}
else
{
//iPad code
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 66, 768, 66);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 130, 768, 66);
}
}
else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 66, 1024, 66);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 130, 1024, 66);
}
}
}
[self.view addSubview:_myIAD];
}
There is a ton of code in there to deal with iOS6.1 and iPads and iPhones, but it works real well. I always like to use the statusBarOrientation to find the orientation, it works better.
As you can see there is a line
_myIAD = [[self appdelegate] myIAD];
I actually make the banner in the app delegate and use it in all the view controllers. To handle rotation, I register for a notification in viewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateUI:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
And basically use the same code in the viewDidAppear in the method updateUI:
The only other thing I did was to put in this code to keep the iAd at the bottom.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = _myIAD.frame;
frame.origin.y = _myTableView.contentOffset.y + _myTableView.frame.size.height-_myIAD.frame.size.height; // Move view to the bottom on scroll
_myIAD.frame = frame;
[_myTableView bringSubviewToFront:_myIAD];
}
I am trying to create 2 iAds but I have only one on simulator. I can not now check it with device. Why I have only one? I have only second iAd
My code is:
adView1 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView1.frame = CGRectOffset(adView1.frame, 0, 50);
adView1.delegate = self;
[self.backgroundView addSubview:adView1];
adView2 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView2.frame = CGRectOffset(adView2.frame, 0, 200);
adView2.delegate = self;
[self.backgroundView addSubview:adView2];
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
if (banner == adView1)
{
banner.frame = CGRectOffset(banner.frame, 0, 50);
}
if (banner == adView2)
{
banner.frame = CGRectOffset(banner.frame, 0, 200);
}
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction)
{
[audio pause];
}
return shouldExecuteAction;
}
(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
[audio resume];
}
adView1.frame = CGRectOffset(adView2.frame, 0, 50);
It must be CGRectOffset(adView1.frame, 0, 50); since adView2 is allocation in the next line only???
When you are creating first view, you are using second view's frame:
adView1 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView1.frame = CGRectOffset(adView2.frame, 0, 50);
adView1.delegate = self;
[self.backgroundView addSubview:adView1];
As adView2 is not created yet - it is nil and frame is (0,0,0,0)
You have to use adView1 frame for offset as you do for second view.
Also, keep in mind that displaying 2 banners is going against Apple guideline and not recommend. Take a look here (Best practices section)
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html