Adding something programmatically to an iOS view - iphone

I am trying to get some AdWhirl ads to work with my app, but I am having a bit of trouble positioning them. The ads work just fine with the following code in my viewDidLoad method:
AdWhirlView *adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
[self.view addSubview:adWhirlView];
The problem is that it displays the add across the top of the view. I am assuming this is because the default drawing area is from 0,0. I don't know how to change the self.view line to draw it towards the bottom of the screen.

Try it like this....
AdWhirlView *adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
adWhirlView.frame = CGRectMake(0.0f, 100.0f, adWhirlView.frame.size.width, adWhirlView.frame.size.height);
[self.view addSubview:adWhirlView];
It should show the new view further below (100 points) - adapt that 100.0f according to your needs.

view.frame = CGMakeRect(x,y,w,h);

Related

Drawing in a nested UIView

I am trying to make a drawing application following this tutorial: http://www.effectiveui.com/blog/2011/12/02/how-to-build-a-simple-painting-app-for-ios/, and then I tried to make it such that I don't only draw on the entire screen, I only draw on a UIView that is inside of another UIView. (What I call a nested UIView)
My code is currently on github: https://github.com/sammy0025/SimplePaint
The only parts I tweaked with the original code from the tutorial is to change some class prefix names, enabled ARC so no deallocs, used storyboards (which works fine with the original code from the tutorial), and change this code in the main view controller implementation file (SPViewController.m):
-(void)viewDidLoad
{
[super viewDidLoad];
nestedView.backgroundColor=[UIColor colorWithWhite:0 alpha:0]; //This is to make the nested view transparent
SPView *paint = [[SPView alloc] initWithFrame:nestedView.bounds]; //original code is SPView *paint=[[SPView alloc] initWithFrame:self.view.bounds];
[nestedView addSubview:paint]; //original code is [self.view addSubview:paint];
}
My question is how do I make sure that I only draw inside the nested UIView?
Add clipping to your sub view. E.g. self.bounds would cover the whole area of a view.
UIBezierPath *p = [UIBezierPath bezierPathWithRect:self.bounds];
[p addClip];
I believe clipping for a view should be based on bounds initially and that you may shrink it by adding an new clipping. But there might be a difference between iOS and OS X here.
Rather than using an SPView inside a nested view, consider just changing the frame of the SPView to match what you want. This should solve your problem of only allowing drawing within a given rect.
I think you're seeing issues because of your storyboard configuration. I dont know much about storyboard, but I was able to get this to work programmatically by throwing out storyboard's view and building my own in viewDidAppear.
-(void)viewDidAppear:(BOOL)animated{
UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
newView.backgroundColor = [UIColor greenColor];
SPView *newPaint = [[SPView alloc] initWithFrame:CGRectInset(self.view.bounds, 40,40)];
[newView addSubview:newPaint];
self.view = newView;
}

positioning frame does not work after move to iOS 4.2

I'm having some trouble with moving a view:
- (void)viewDidLoad {
CGRect newFrame = self.popUp.view.frame;
newFrame.origin.y = self.view.bounds.size.height;
self.popUp.view.frame = newFrame;
[[self view] addSubview:[self.popUp view]];
[super viewDidLoad];
}
This should put the subview popUp below the current screen but it does not seem to be moving it. I'm almost positive this was working pre-4.2. Any ideas as to what might be going on? Sorry for the vagueness. If you have any questions feel free to ask.
Did you try moving [super viewDidLoad] to the top of the method? Where you call a superclass's method can affect the state of inherited vars.
Good post on this: `[super viewDidLoad]` convention
It looks like you are moving so the top of popUp.view is at the bottom of self.view. And then when you are adding popUp.view to self.view you are placing it "outside" of self.view. You can verify this by newFrame.origin.y = self.view.bounds.size.height - 100; and see if it shows up.
One reason that your view doesn't show up as it did before could be that you have changed clipsToBounds. I imagine it can also be some other drawing optimization taking place and not drawing views that are out bounds and not visible.
If you want your pop up to show up below a view I think a better approach would be to either place your pop up at the bottom of your view:
newFrame.origin.y = self.view.bounds.size.height - newFrame.size.height;
or by placing the pop up view in the superview of self.view. In that case you would probably want to handle the showing in the controller of the superview.

Adding adWhirl view with a MKMapView

I am trying to implement an adWhirl view in the same view as one that has an MKMapView object. If I follow the standard steps that work fine with my tableViews, that is
awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
[self.view addSubview:awView];
then the funny behavior starts. In the simulator it receives the touch and sends you on. But when running on my test device (running 3.1.3) the touch passes through to the map.
I have been told that this is because awView is being cast as a subView of mapView and that it must be its own view. But how? I have tried creating a separate UIView and then placing awView in it, then locating it at a fixed location, but instead of the fixed location, it loads relative to mapView and still does not receive touches.
Any suggestions?
Addendum: I thought that I was adding both as subviews but have not had any success. What I had done is to create two views in IB. The top one (firstView) has two subviews (bannerForAd and mapView.) This is what I have now
self.awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
self.awView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[bannerForAd addSubview:awView];
mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
mapView = (MKMapView*)self.view;
mapView.delegate = self;
[firstView addSubview:mapView];
[firstView bringSubviewToFront:bannerForAd];
Now the map shows fine, but the ad is not even visible.
Stick both the AdWhirlView and the MKMapView as a subview of a UIView.

Transparent NavgationBar on iPhone

I'm trying to display transparent UINavigationBar on top of Scrollview.
This is actual result of code that I have written...
where as I'm expecting view to be displayed like below image, which happens after I slightly scroll the image.
Code :
- (void) loadView {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [self imageCount],
pagingScrollViewFrame.size.height);
pagingScrollView.delegate = self;
self.wantsFullScreenLayout = YES;
pagingScrollView.scrollsToTop = YES;
self.view = pagingScrollView;
}
question is how do I make view to load as I expected without user interacting to it?
any suggestion or help is appreciated..
EDIT: I'm creating view totally from CODE
It seems like you're trying to do this in code not in the IB. If so, you have to put your code in the viewDidLoad of the Application Delegate (e.g. MyProgramAppDeligate class or whatever). If you want it in some certain views, put it in the viewDidLoad of the UINavigationController class/subclass.
Does this satisfy your requirement?
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
And to make your statusbar translucent.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
[self setWantsFullScreenLayout:YES];
I have this inside my willWillAppear and I reset it in my viewWillDisappear.
You have to set
self.navigationController.navigationBar.translucent = YES;
As soon as you do this, the 0,0 coordinate is behind your navigation bar not below it and your view shifts behind the bar.
It may be a conflict between IB and your code. I would add the line of code suggested by Ortwin in the viewDidLoad method and then double check you've set the navBar to translucent in IB.
The code you have posted has nothing to do with the opacity of the navigation bar. Show where you are setting the configuration of the components of the view. There you could just set the alpha of the navigation bar. Alternatively if you are using nibs, just set the alpha in IB.
Since you say that it works fine after the user (you) slightly scrolls the image, the problem might be that the UINavigationBar's drawRect: method does not get called after the UIScrollView is loaded.
Suggestion: Can you explicitly call setNeedsDisplay on the navigation bar after the view is loaded?
Have you tried setting the frame of the scroll view with an Y origin of 0 after setting the nav bar to transparent?
EDIT: I mean, you don't say what's the frame used in your code.
In viewDidLoad, try moving the origin up 32 pixels and grow the height by 32 pixels as well:
pagingScrollView.frame = CGRectMake(pagingScrollView.frame.origin.x, pagingScrollView.frame.origin.y-32, pagingScrollView.frame.size.width, pagingScrollView.frame.size.height+32);
In viewWillAppear scroll the content to the correct location.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIScrollView *scrollView = (UIScrollView *)self.view;
[scrollView setContentSize:CGSizeMake(320,568)];
[scrollView scrollRectToVisible:CGRectMake(0, 548, 320, 20) animated:NO];
}

addSubview outside resized frame

I've coded a "generic" ads management class for all my applications and i have an issue. This class can add an ads view to any view of my application, randomly; in order to do so, my idea is to resize the frame of my current view to reduce its height (let's say 50 pixels less) and add my ads view in the free space i created. This way, i don't have to bother modifying my views for ads integrations, everything is done automatically. It's working well but my ads aren't responding to touch events. I guess it's because this ads view is "outside" the frame of my controller.
It is possible to reduce the height of my view frame and raise its bounds so my ads subview is really part of my view?
Thanks a lot :)
UIView *adView = [[UIView alloc] init];
adView.frame = CGRectMake(0,267,320,100);
adView.backgroundColor = [UIColor grayColor];
adView.tag = 123456;
adView.userInteractionEnabled = YES;
CGRect myFrame = [self.view frame];
myFrame.size.height = myFrame.size.height - 100;
[self.view setFrame:myFrame];
[self.view addSubview:adView];
Here's a picture representing what i would like to do :
http://i49.tinypic.com/2iw7lz4.jpg
This is not an answer but I do not have option to post a comment to your question.
Can you please post the code you have in your touchesBegan method?
I think there can be the problem
Regards
Alejandra