How to create a UIView? - ios

I'm following a tutorial (https://github.com/MrCapone/MyAdMobController-iOS) to add banner in my app, but I don't understand a thing, here:
"FOR SHOW BannerView FIRST CREATE A UIView AND ADD IT TO TOP OF ROOT
UIView :
UIView *adView = [[UIView alloc] initWithFrame:adRect];
[[CCDirector sharedDirector].view addSubview:adView];
THEN ADD BannerView TO IT:
[[MyAdMobController sharedController] addBannerToView:adView];
where he says to create a UIView, It means that I have to create a new scene? because i don't know exactly what is a UIView (i'm a beginner), someone can give me an example of what i have to do?

The line:
UIView *adView = [[UIView alloc] initWithFrame:adRect];
Shows you how a view is created. And no a UIVIew is not a Scene.
A view is the most simple building block of iOS interface components.. Basically just a rect angle..

Related

send programmatic UIView to back against UIView added in Storyboard

I'm creating UIView programmatically and called this UIView captureImage. This UIView captureImage display the image that I capture from camera and the parentView I called it self.frameForCapture. But the problem is, It always overlap the UIView that I add to the storyboard.
Update Post:
Here is the image that I'm trying to make in my app.
Black View is my parentView and I called it self.frameForCapture.
Blue View is childView, and I add it using the storyboard.
Red View is childView, and I programmatically added to the parentView.
But the time I add it the redView. It always overlap the blueView, i use this addSubView:.
But if I used these
[self.frameForCapture bringSubviewToFront:captureImage];
[self.frameForCapture sendSubviewToBack:captureImage];
nothing happen.
I used this code:
self.newalbumImageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.newalbumImageView setContentMode:UIViewContentModeScaleAspectFill];
self.newalbumImageView.image = self.albumImage;
UIView * captureImage = [[UIView alloc] initWithFrame:self.frameForCapture.frame];
[captureImage addSubview:self.newalbumImageView];
[self.frameForCapture addSubview:captureImage];
there are a few things that can help you look at this question How to make a UIView always appear at the front?
just link the headerview to .h file and use myAlwaysOnTopView.layer.zPosition = MAXFLOAT;
another thing is that when you use [self.frameForCapture sendSubviewToBack:captureImage];
the redview will still be on top of self.frameForCapture if don't want it like that you can hide it or even remove
if you still have this problem just tag me

Pass Subview to subclassed UIView

I have a subclassed UIView with a special behavior.
It is a custom class of a ViewController xib view. It contains a subview, which is currently created programmatically.
My question is; how to pass a subview either from the xib or from one created programmatically in the viewController via initWithFrame or initWithCoder to my subclassed view? (Right now it simply acts through the view, not initiated at all in the VC.)
I would share code, but I don't know that it's necessary.
Use the initWithFrame method:
SubclassedView *view = [[SubclassedView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:view];
Be sure to import #import "SubclassedView.h"

Subclass UIView from ViewController

I'm trying to show an onscreen tutorial (like a picture with hints) in my viewController. I only know how to "open" a UIView with its drawRect method, where my paint code is inside, from the AppDelegate with:
BannerView *view = [[BannerView alloc] initWithFrame:self.window.frame];
[self.window addSubview:view];
[self.window makeKeyAndVisible];
Is it possible to activate the UIView (BannerView) by a button from inside a ViewController?
Thank you very much!
Simply use this code:
BannerView *view = [[BannerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:view];
in your button action method (if it is defined in your controller class). See this S.O. question to know how to create a button programmatically. Otherwise, you can use interface builder for that.
Using self.view.bounds in initWithFrame will make your banner view as large as the controller's view (which could be smaller than the display).
I believe what you are looking for is adding an event handler to a button on the view controller that will create the view and add the view by using something like
BannerView *view = [[BannerView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[self.view addSubview:view];
where self is the view controller.

Replacing self.view with new UIView shows black view

I want to change the existing view in a UIViewController to a new view. The new view contains the old view and a little banner view.
Doing this fairly simple change leaves me with a black view.
My code looks like this
UIView *existingView = self.view;
UIView *newView = [[UIView alloc] initWithFrame:existingView.frame];
UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 50), 320, 50)];
CGRect existingViewFrame = existingView.frame;
existingViewFrame.size.height -= 50;
existingView.frame = existingViewFrame;
[newView addSubview:existingView];
[newView addSubview:bannerView];
self.view = newView;
However when switch Tabs and come back to the view which changed the view is shown just like I want. I guess I need to set a flag or something to tell the controller to redraw it's (new) view.
Edit
I wrote an simple example for this problem. You can find it on GitHub: https://github.com/Oemera/ChangeView
You did not say where you do this. It may be that you need to save the original view's super view, then add the new view to that views subViews array. I'm betting that is the problem.

UIScrollView won't show in UIView

My app is building purely programmatically on UITabBarController above a UINavigationController, which are both declared in my AppDelegate. Inside my navigationcontroller, I'm showing a UIViewController, a custom class. This custom class should show a custom UIView in a UIScrollView and a UIPageControl.
Heres my problem:
self.view seems to create an EXC_BAD_ACCESS error when I call it without declaring self.view = [[UIView alloc] init] (or similar). I was wondering if this was a problem with -(void) loadView but seems like it produces the same error in -(void)viewDidLoad. So I basically had to use self.view = scrollView to even show my scrollView, considering [self.view addSubview:scrollView] produced an error. My UIPageControl should stay on the page all the time, and actually be another part of the view than the UIScrollView. So I tried to add a container-view like this
Code:
container = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,0,0)];
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
// EDIT: ofcourse, I'm also resizing the frame with [scrollView setContentSize:] later, but this is farfetched code to add here.
[container addSubview:scrollView];
self.view = container;
Unfortunately, it seems that I don't get any result at all, and what appears is just an empty view. However, if I add a UILabel or similar, it shows:
[container addSubview:[[UILabel alloc] initWithFrame:CGRectMake(50,50,50,50)]]; // entered above or beneath the addSubview:scrollView line.
My question is: Why doesn't my scrollView appear in the UIView container?
I've noticed that some tutorials say that scrollView must have a delegate, and I agree with the logic - however I can't seem to find out how I set that delegate when I am in my CustomUIViewController-class instead of my AppDelegate.
after you change the UIScrollView size you should use:
[scrollView setNeedsDisplay:YES];
also you implement Delegates the same way you do in other classes:
.h:
#interface MyClass : NSObject <UIScrollViewDelegate>
Okay, the problem seemed to be the initialization - I didn't realize that frame and content was two different things. Seems like the frame that is initializing the view should be whatever size the view should fill, while content is the actual content of whatever should be scrolled. So when I was having problems with user interaction, it was really this.
The problem of why it didn't show in the first place was (stupid.) that the frame was initially, and never changed from, 0,0 so I really lied in my first post.
Thanks to UIScrollView and PageControl: space between views who solved my problem with user interaction.
My steps was to backtrace from self.view:
NSLog(#"%f\n%f",
((UIScrollView*) [[self.view subviews] objectAtIndex:0]).frame.size.width,
((UIScrollView*) [[self.view subviews] objectAtIndex:0]).frame.size.height);
when I realized these were 0 and 0, fixing the problem wasn't too hard :) thanks though, for your efforts Kristian.

Resources