I am trying to use the Inneractive Ad SDK 5+ in my application and I'd like to position the ad on the bottom. In every case I've tried it will always be in the middle of the screen, centred horizontally and vertically.
Code:
self.myAdView = [[IaAdView alloc]
initWithAppId:#"MyAppID_iPhone" adType:IaAdType_Banner delegate:self];
self.myAdView.adConfig.refreshIntervalInSec = 30;
[[InneractiveAdSDK sharedInstance] loadAd:self.myAdView];
Delegate method:
- (void)InneractiveAdLoaded:(IaAd *)adView {
NSLog(#"AD LOADED METHOD CALLED");
// Here I tried many things I found online without any effect
[self.view addSubview:self.myAdView];
}
Here how i implement the property at the top of the viewcontroller.m file:
#interface ViewController () <InneractiveAdDelegate>
#property (nonatomic, retain) IaAdView* myAdView;
#end
#implementation ViewController
#synthesize myAdView = _myAdView;
I am sure it must be just a tiny thing but I can't figure it out. I found many solutions that include initWithFrame but this is not usable in this case.
Any help would be appreciated.
Update 1:
I've tried:
- (void)InneractiveAdLoaded:(IaAd *)adView {
NSLog(#"AD LOADED METHOD CALLED");
self.myAdView.frame=CGRectMake(x,y,width,height); // With any number and random number
and
self.myAdView.frame=CGRectOffset(self.myAdView.frame, 0, yOffset); // With any number and random number
[self.view addSubview:self.myAdView];
}
Nothing helped. Ad stays absolutely centred. I am starting to think there is something wrong with the setup but besides the positioning everything is working fine. See photo below.
Update 2:
Tried the following without success. ios8 shows banner centred on screen and ios7 for some reason not at all once the line has been added.
- (void)InneractiveAdLoaded:(IaAd *)adView {
NSLog(#"AD LOADED METHOD CALLED");
self.myAdView.frame = CGRectMake(0, self.view.frame.size.height - self.myAdView.frame.size.height , self.view.frame.size.width, self.myAdView.frame.size.height);
[self.view addSubview:self.myAdView];
}
Here's a working solution:
You have two options here -
Wrapping your adView in the external UIView and then you have the full control
Updating the frame of adView on the 'InneractiveAdLoaded:' event
ANYWAY: adView must be added to it's superView before calling 'loadAd:' SDK method.
Let me know, if you have any questions.
Update:
Option 2:
#import "ViewController.h"
#import "InneractiveAdSDK.h"
#interface ViewController () <InneractiveAdDelegate>
#property (nonatomic, strong) IaAdView *adView;
#end
#implementation ViewController {}
- (void)viewDidLoad {
[super viewDidLoad];
self.adView = [[IaAdView alloc] initWithAppId:#"<Your ad unit ID>" adType:IaAdType_Banner delegate:self];
[self.view addSubview:self.adView];
[[InneractiveAdSDK sharedInstance] loadAd:self.adView];
}
- (void)positionAd {
const CGFloat x = (self.view.bounds.size.width - self.adView.frame.size.width) / 2.0f;
const CGFloat y = self.view.bounds.size.height - self.adView.frame.size.height;
self.adView.frame = CGRectMake(x, y, self.adView.frame.size.width, self.adView.frame.size.height);
}
#pragma mark - InneractiveAdDelegate
- (void)InneractiveAdLoaded:(IaAd *)adView {
[self positionAd];
}
- (void)InneractiveDefaultAdLoaded:(IaAd *)adView {
[self positionAd];
}
- (UIViewController *)viewControllerForPresentingModalView { return self; }
#end
Try setting its frame this way:
self.myAdView.frame=CGRectMake(x,y,width,height);
Where x,y,width,height is your desired position and size.
Or if its already positioned nicely by X coordinate, width and height you could use:
self.myAdView.frame=CGRectOffset(self.myAdView.frame, 0, yOffset);
Where yOffset is how much you want it moved on that coordinate. Be sure you calculate it using self.view.frame and not with exact number, so its on bottom on every device.
To position your banner at the bottom of your UIView you need to change its frame.
self.myAdView.frame = CGRectMake(x, y, width, height);
self.myAdView.frame = CGRectMake(0, self.view.frame.size.height - self.myAdView.frame.size.height , self.view.frame.size.width, self.myAdView.frame.size.height);
x - Set to origin of 0
y - Set origin to the UIView's height. But this will make it appear off the bottom of the screen. So, we must subtract the height of our self.myAdView to move it up onto the screen: self.view.frame.size.height - self.myAdView.frame.size.height
width - Set it to the width of the UIView: self.view.frame.size.width
height - Set it to the height of our self.myAdView: self.myAdView.frame.size.height
Related
I have a CustomScrollView which holds a list of UILabels and scrolls through them (the subclassing is for automatic positioning of the views). In the class I have a list of these views stored as an NSMutableArray. I have found that when I ask for label.view.frame.origin after getting the label from the array, I always get {0, 0}. Because of this, the logic I use to scroll does not work (the scroll is done programmatic-ally). I am trying to have the UILabel scroll to the center of the frame of the CustomScrollView. Here is a code sample to show where I am having problems:
#interface CustomScrollView : UIScrollView
#property (nonatomic) NSMutableArray* labels; //This is the array of UILabels
#end
#implementation CustomScrollView
-(void)scrollToIndex:(int)index withAnimation:(bool)animated
{
UILabel *label = (UILabel*)self.labels[index];
CGRect rect = CGRectMake(label.frame.origin.x - ((self.frame.size.width - label.frame.size.width) / 2), 0, self.frame.size.width, self.frame.size.height);
[self scrollRectToVisible:rect animated:animated];
}
#end
TL:DR - label.frame.origin.x is returning me 0 and not what it's relative position is within the superview.
Bonus - Whenever I instantiate my custom scroll view, it automatically adds two subviews which I have no idea where the come from. I set the background color and turn off the scroll bars and scroll enabled.
Thanks in advance for the help.
Edit [Jun 25 11:38] - Turns out the rect I am creating to scroll is correct, but calling [self scrollRectToVisible:rect animated:animated] is just not working.
Edit [Jun 25 12:04] - Here is the method which I call every time I add a subview
-(UILabel*)lastLabel
{
if (self.labels.count == 0)
return nil;
return (UILabel*)self.labels.lastObject;
}
-(void)adjustContentSize
{
if (self.labels.count > 0)
{
float lastModifier = [self lastLabel].frame.origin.x + [self lastLabel].frame.size.width + ((self.frame.size.width - [self lastLabel].frame.size.width) / 2);
self.contentSize = CGSizeMake(MAX(lastModifier, self.contentSize.width), self.frame.size.height);
}
}
Try using the convertRect function:
CGRect positionOfLabelInScrollView = [self.scrollView convertRect:myLabel.frame toView:nil];
[self.scrollView setContentOffset:CGPointMake(0, topOfLabel - positionOfLabelInScrollView.origin.y + positionOfLabelInScrollView.size.height) animated:YES];
This should scroll your scrollView to display the label.
so there must be something simple I am missing because I just can't figure out how to move my UIView
This is what I have so far
my .m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self buttonLayout];
}
-(void)buttonLayout
{
UIView *theView = [self buttonGroup];
theView.center=CGPointMake(50, 50);
}
My .h file
#property (nonatomic, retain) IBOutlet UIView *buttonGroup;
So this is what I have so far and i just cant seem to get it to move at all
P.S i don't want it animated as i'm getting my layout to move to compensate for different iPhone screen sizes :)
I have not tested this, but it seems like you need to:
A) Remove the view from its parent.
B) Adjust the center of its 'frame', not the view.
C) Re-add the subview.
Like so:
UIView *theView = [self buttonGroup];
CGRect theFrame = theView.frame;
[theView removeFromSuperview];
theFrame.origin.x = 50.0;
theFrame.origin.y = 50.0;
theView.frame = theFrame;
[self.view addSubview:theView];
This should work with autolayout.
As the other poster says, you can't move views around by changing their center or frame when you have auto-layout in effect.
Instead, you have to add a vertical and horizontal constraint that positions the view from the top/left edge, connect an outlet to the constraint, and then change the constant value on the constraint from code.
I had similar problem , but when i tried to change the origin as suggested in first solution , i got "Expression not assignable" error.
so i solved it with below solution in viewDidLoad().
First save its center in some temp variable , then change the centre and add again to its parent view.
CGPoint buttonCenter = self.yourButton.center;
[self.yourButton removeFromSuperview];
buttonCenter.y = buttonCenter.y + 50.0;
self.yourButton.center = buttonCenter;
[self.view addSubview:self.yourButton];
I have a view that looks like the following:
The red rectangle (referencing outlet: self.redView) is put inside of a scrollview (referencing outlet: self.scrollView). When the "Bigger" or "Smaller" buttons are pressed, the red rectangle expands / contracts its height.
My code is here:
#import "s1cViewController.h"
#interface s1cViewController ()
#end
#implementation s1cViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidLayoutSubviews {
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width, 600)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnBiggerTouched:(id)sender {
[self resizeViewHeight:20];
}
- (IBAction)btnSmallerTouched:(id)sender {
[self resizeViewHeight:-20];
}
- (void)resizeViewHeight:(int)heightDelta {
[UIView animateWithDuration:0.2f animations:^{
// Resize scrollview height
/*self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.scrollView.contentSize.height + deltaInputWrapperHeight);*/
// Resize categoryInputView height
CGRect rect = self.redView.frame;
rect.size.height += heightDelta;
self.redView.frame = rect;
}];
}
#end
When I change the height of the rectangle, and then scroll, the height changes back to its original size. How do I prevent this?
Perhaps you are using auto layout. If so, that's the problem. You cannot change a view's frame directly under auto layout, because if you do, when layout comes along, it will just change it right back (as seems to be happening here). You have to change the constraints.
In Xcode 5.1 I have created a very simple single view app for iPhone and put the source code at GitHub:
I have disabled Autolayout and put the following views in each other: scrollView -> contentView -> imageView (here fullscreen):
For the contentView and imageView I've disabled Autoresizing and set their frames to {0, 0, 1000, 1000} - both in the Storyboard and in the viewDidLoad method.
I have enabled double tap and pinch gestures for zooming.
For double tap the image is zoomed at 100% or 50% width.
This works initially, but after device rotation it breaks:
The zoom doesn't work properly and the image is offset - you can't scroll to its top left corner:
Here is my very short code in ViewController.m, please advice how to fix it:
- (void)viewDidLoad
{
[super viewDidLoad];
_imageView.frame = CGRectMake(0, 0, 1000, 1000);
_contentView.frame = CGRectMake(0, 0, 1000, 1000);
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
float scale = _scrollView.frame.size.width / 1000;
_scrollView.minimumZoomScale = scale;
_scrollView.maximumZoomScale = 2 * scale;
_scrollView.zoomScale = 2 * scale;
_scrollView.contentSize = CGSizeMake(1000, 1000);
}
- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
return _contentView;
}
- (IBAction)scrollViewDoubleTapped:(UITapGestureRecognizer*)sender
{
if (_scrollView.zoomScale < _scrollView.maximumZoomScale)
[_scrollView setZoomScale:_scrollView.maximumZoomScale animated:YES];
else
[_scrollView setZoomScale:_scrollView.minimumZoomScale animated:YES];
}
UPDATE: I've tried using Reveal app (here fullscreen), but couldn't find anything useful for me:
My source code seems to be okay, but in Interface Builder I had to disable "Autoresize Subviews" for the scrollView:
I have the following which works fine for somethlike a UIImageView. I am not sure if a UIView animation would work with a contained controller? I know I am animating the view's frame but just not sure if this would / should work (the below code is just proof-of-concept). But even if it did, the below is not working. Any ideas on why not?
thx in advance
edit #1
I've added a screen shot that shows a custom view controller in yellow with the phrase holla in it. As you can see the below code aniamtes the frame but not the elements of that view controller. Is this intended? Is there a workaround?
#import "MyViewController.h"
#interface ViewController ()
#property (nonatomic, strong) MyViewController *firstIV;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
// lets add a few UIImageViews that we will animate
MyViewController *tmpFirstIV=[[MyViewController alloc] init];
tmpFirstIV.view.backgroundColor=[UIColor yellowColor];
tmpFirstIV.view.frame=CGRectMake(10.0f, 10.0f, 100.0f, 100.0f);
self.firstIV=tmpFirstIV;
[self addChildViewController:self.firstIV];
[self.view addSubview:self.firstIV.view];
[self.firstIV didMoveToParentViewController:self.firstIV];
-(void)updateAction:(id) sender{
NSLog(#"Here are some results");
if([self.updateButton.currentTitle isEqualToString:#"update this"]){
[UIView animateWithDuration:6.0f animations:^{ // yeah I know, long animation
self.firstIV.view.frame=CGRectMake(10.0f, 10.0f, 100.0f, 0.0f);
}];
[self.updateButton setTitle:#"make bigger" forState:UIControlStateNormal];
}else{
[UIView animateWithDuration:6.0f animations:^{ // equally long
self.firstIV.view.frame=CGRectMake(10.0f, 10.0f, 100.0f, 100.0f);
}];
[self.updateButton setTitle:#"update this" forState:UIControlStateNormal];
}
}
edit2
doesn't seem to be doing anything... will look into this layer.
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
CGRect viewBounds = self.view.bounds;
self.myLabel.frame = CGRectMake(100.0f,viewBounds.size.height-20.0f,50.0f,50.0f);
}
You are only animating the frame of the yellow box, not the label. The frame of the label will not change just because its superview's frame has changed. You either need to:
Write code to animate both objects when the button is tapped
Write code to automatically move the label when the box is resized, typically by overriding your view controller's - (void)viewWillLayoutSubviews method.
If you're targeting iOS 6 only, you can use the new auto-layout feature. Ray Wenderlich has an excellent tutorial.