Rather new to trying to get OpenGL ES working on the iPhone, but I've created a working demo from an OpenGL template and wanted to add it to another App which is a standard UIViewController App. After searching around it seems that I can't addSubview a CAEAGLLayer to a CALayer aka UIView. How would I go about doing this? Or am I completely wrong in what I'm doing.
Cheers for any help
Simply use a UIView and add:
+ (Class)layerClass {
return [CAEAGLLayer class];
}
That UIView is now a CAEAGLLayer, so you can add that view as a subview of another UIView like normal.
What I actually had to do was change in the EAGLView -initWithCoder to -initWithFrame and make the appropriate changes to the method to handle the frame. Once I did that and initialised the view with initWithFrame it worked.
I believe you are looking for CALayer's insertSublayer methods or the addSublayer method.
Related
Is there an easy way for listening for when a child or subview has been added to a UIView?
I've gone through the addobserver options and haven't found an obvious option anyway. There may be another option that would be affected though when content is added to a view or am I wrong in saying that? i.e. content width or height, positions, etc.?
Edit
This is accomplished easily using the advice of #Alkimake below (TextHolderClass).
I created a custom UIView subclass and set the UIView's class in Interface Builder to be equal to TextHolderClass
Thanks for your help, I know it should have been obvious :)
UIView methods may help you:
- (void)willMoveToSuperview:(UIView *)newSuperview
UIView has 2 methods to call after subview interactions. Simply create your custom UIView class and implement these methods which is pretty for you. And use your own CustomView
- (void)didAddSubview:(UIView *)subview;
- (void)willRemoveSubview:(UIView *)subview;
Can't seem to find a way to fix a graphic - a light graphic that would remain static as the user navigates from scene to scene. Have been combing forums for a few days, but most answers point to MainWindow.xib, which isn't generated for a storyboard project. Would appreciate any advice.
Here’s what I was able to cobble together thanks to advice from #Max. Like I said, very new to this, so if this code needs tuning please leave a comment so I don’t lead others astray.
Add the image to the app’s main window in the AppDelegate’s didFinishLaunchingWithOptions method :
UIImageView *myGraphic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myGraphic.png"]];
[self.window.rootViewController.view addSubview: myGraphic];
[self.window.rootViewController.view sendSubviewToBack: myGraphic];
Then, to make your views transparent so the background shows through - in your viewController class/es, add to viewDidLoad method:
self.view.backgroundColor = [UIColor clearColor];
(And note if you try to decrease the transparency through the alpha property for the view via the Attributes Inspector, it will make everything transparent on that view -buttons, etc - that’s why it needs to be done programmatically.)
Sure, it’s not thoroughly tested. But for now it’s working and it’s a thing of beauty. Thanks all.
You can try to manually add UIImageView to your window and then set 0 background transparency
for all other views.
One way to do it -- it may not be the best -- can be that you subclass a UIView, and set the UIView instance in all of your viewControllers in the storyboard an instance of your cutomized UIView class, which contains your graphic as a background.
If you're going to tackle this programmatically, within each view controller, set:
[[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
in viewDidLoad
I'm sure some tweaking would allow you to apply this principle throughout the program from the app delegate, or by using a singleton.
Using this solution, though, you're still going to have to make some method call within each view controller where you want this style applied.
I was really impressed, and really grateful about the answers I recived last time I asked here.
I have this problem with Core Plot.
I want to have a CPTGraphHostingView inside my UIView so I can have things like labels and scroll views below it.
I am using XCode 3.2 by the way.
How do I do this programmatically? Or with the Interface builder if possible.(I need the instructions to be detailed as im I bit new to this sort of thing)
Thanks for your support.
If you want to add a core plot graph to a UIView you have to add a CPTGraphHostingView as a subview to the UIView. In the example below, hostingView is a UIView and graphObject is a CPTXYGraph.
CPTGraphHostingView *graphHostingView = [[CPTGraphHostingView alloc] initWithFrame:hostingView.bounds];
[hostingView addSubview:graphHostingView];
graphHostingView.hostedGraph = graphObject;
Simple adding CPTGraphHostingView as a subView of UIView [programatically] didn't work for me.
I have searched through many threads and at the end I tried the simplest way to do this - I have created new UIView in interface builder and in this view I have created another UIView and change it to CPTGraphHostingView. Now I can simply create chart [like in tutorials] and link its main UIView anywhere I want.
I don't know why this didn't work programatically but it works from Interface Builder.
I use Xcode 4.2 and Core plot 1.0
I show bg of my app using UIImageView. To support correct interface rotation i write layoutSubviews method. In this metod .frame property changes correctly, but when i try change image of UIImageView just like this:
view = [self viewWithTag:1002];
((UIImageView *)view).image = [UIImage imageNamed:#"portrait.png"];
nothing changed. Did I do something wrong or is it impossible to change image of UIImageView in layoutSubviews method?
try this
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// change background here
}
You manage the rotation of your views by using a UIViewController (be sure to check out the docs here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html). If you implemented a UIViewController for your UIImageView you could set the image in the viewDidLoad: method.
You shouldn't override the layoutSubviews: method to perform this action. layoutSubviews:is to do what it says in the name - it should perform the positioning and sizing of any subiews that this view currently has. You shouldn't need to override this method unless you wanted to do some special custom laying out of the subviews and is called in the run loop when the UIView is set to "requires resizing".
I strongly suggest you set your image from from some other location in the code. If you want the image to change when the view is rotated, you could set the image in the - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method of a UIViewController subclass.
I am subclassing a UIView and overwrite the drawRect method. I'm noticing that the view's drawrect is only being called when the view first loads. After that it is never called again. How to I make sure it gets called after an orientation change? I've tried calling setNeedsDisplay on the view before and after the rotation and that doesn't do it.
[myView setContentMode:UIViewContentModeRedraw];
You can set this in IB as well (i.e., set mode to "redraw")
This was a bug related to something completely different. setNeedsDisplay does indeed cause drawRect to be called.
to answer this and the other 94,000 similiar questions about view rotation/drawrect funkiness,
-(id)initWithFrame:(CGRect)frame
{
if(self=[super initWithFrame:frame]) {
self.autoresizesSubviews=YES;
self.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
}
return self;
}