UIView: hide drawing and show it again - xcode

I create a view at run time and I use it's drawRect: to draw a figure on it. In the next step I add a sublayer with contents of an image to the layer of the view and then I show it. It works. But the figure on the view is still shown below the image. With view.layer.contents = nil; before creating the sublayer, the figure on the view is removed. But I need to show it again. With [view setNeedsDisplay]; (calling drawRect:) I can draw it again.
Is there any (easier) way to hide (or cover) this figure and show it again (without removing and redrawing it)? Thanks.
Edit:
I can do this: layer.backgroundColor = [UIColor blackColor].CGColor; the figure is covered. But the backgroundColor must be transparent. Thanks very much for another ideas.

You can use:
view.layer.hidden = YES;
If you only want to hide and show a sublayer, you can keep hold of a reference to that sublayer, or find it by looking in view.layer.sublayers.

Related

Xcode changing constraints dependant on screen orientation

I have designed an app using auto layout and all works perfectly, issue is, when I switch to landscape I want the location of one button to move to the other side of the screen.
Im assuming the best way of doing this is by changing the constraints when I rotate the screen, is this possible in the code, or even the best way of doing it? Obviously if the app starts up in landscape I obviously want this button to be in the landscape mode also.
thanks
Absolutely. Remove and add constraints as desired in your view controller's updateViewConstraints implementation.
You will find it easiest to prepare both sets of alternate constraints beforehand, maintaining them in instance variables. Then you can just swap them in and out. Here's a sketch of what the code could look like:
-(void)updateViewConstraints {
[self.view removeConstraints:self.oneSideConstraints];
[self.view removeConstraints:self.otherSideConstraints];
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
[self.view addConstraints:self.oneSideConstraints];
else
[self.view addConstraints:self.otherSideConstraints];
[super updateViewConstraints];
}

UIImageView not updating image when displayed in a UIScrollView

I am new to Xcode and objective C so the problem I have hit may be a simple one but I haven't been able to find an answer yet so thanks for any help you can give.
I am trying to write a simple app that takes a picture from the camera and displays it in a UIImageView. This all works fine if the UIImageView control is just placed in the UIView control (using the interface builder). The code I use to set the image to the control is
[self.imageViewOSFCorner setImage:image];
However I need to display several images an wanted the user to be able to scroll up and down the page. So I created a UIScrollView in the interface builder that is the size I need and placed all the controls in that. Then in the viewDidLoad method I placed the following code to display the UIScrollView
self.scrollView.contentSize = self.scrollView.frame.size;
self.scrollView.frame = self.view.frame;
[self.view addSubview:self.scrollView];
This works in that you can now scroll the page and see all of the UIImageView controls, and when I press the button it launches the camera, but now after taking the picture it doesn't display the image into the UIImageView. If I take the UIScrollView away it works again (but obviously doesn't scroll) - I am guessing I am doing something silly, is there a function I need to call to update the UIScrollView?
As an aside I originally tried to use the code
self.imageViewOSFCorner.image = image;
to display the image but this wouldn't display the image and had to use
[self.imageViewOSFCorner setImage:image];
can someone tell me why the first method didn't work?
Thanks
Will
May be useful:
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width*num,view.frame.size.hight );
num is your number of all image
Ok it is working now, I must have done something stupid when creating the controls into the UIScrollView since I have started from scratch again in the interface builder and now it all works.

How to animate a tabbarbutton image

I have a toolBarItem which calls a save action on a database. I would really much like to change the image of the item (a cabinet) dynamically so that a drawer opens, a label is animated "inside" and then it closes. Very much like the trash item on the mail app animation.
I know how to make a UIView pop-up by scaling it up and down on an animation on a given times given in an array (together with an array of CT scale), so I'm guessing it could be done more or less the same way.
Does anyone know about an example of who to accomplish that?. Back on xcode 4.1 i was able to highlight the button while the label was moving, but I cannot do that anymore (somehow I did add a normal button on the toolbarItem, which I cannot do anymore).
Thanks in advance!
well, if somebody is having the same issue here is how it can be done:
- (IBAction)barButtonAction:(UIBarButtonItem *)sender {
NSArray *frameArray=[NSArray arrayWithObjects:[UIImage imageNamed:#"01-refresh.png"], [UIImage imageNamed:#"02-redo.png"], [UIImage imageNamed:#"03-loopback.png"], [UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"], nil];
self.button.image=[UIImage animatedImageWithImages:frameArray duration:10.0];
}

Give NSWindow a background image

Ok, so I've created an image in Photoshop that will align with the buttons on my app, and now I'd like to make it the background image of my window so that the characters on the image will correspond to the keys on my app (a small calculator demo app I've been working on)
Basically, instead of giving buttons Text like 1,2,3,4, etc. I've made a 3x3 map with numbers of a different font just because it will look pretty
What I'm having difficulty with now is that I can't seem to make the image the background of my window.
I created an NSImageView and I dragged the image file onto it, so I can see it now, but I can't make it the background.
Do I need to subclass the NSImageView or is there some simple method?
I'm using XCode 4, btw
Thanks!
I think you should be able to do something like:
[window setBackgroundColor:[NSColor colorWithPatternImage:[NSImage imageNamed:#"myImage.png"]]];
This is something that a layer-backed window would be good at:
[[window contentView] setWantsLayer:YES];
[[window contentView] layer].contents = myImage;
I think you stand a better chance of getting this to resize sensibly (assuming you need to) than with a pattern color.
Just to give you more options -- you should be able to [window setContentView:myImageView]. In the .xib file you'd want to add your buttons etc. as subviews of the image view.
I don't necessarily recommend this approach, but it's something to think about.

Creating a checkbox programmatically using Cocoa

I need to create a checkbox programmatically in Cocoa and when I try and make a button with buttonType set to NSSwitchButton it displays the title, but does not show the button as a checkbox. I think I am missing something but I can't find any resources about making things like checkboxes without using the Xcode GUI.
The question is a little old so you've probably already figured it out, but I found it while searching for this exact thing. Alex danced around the solution without actually providing it. So here, for Google and all mankind: how to programmatically create a checkbox in Cocoa.
NSRect frame;
frame.size.width = frame.size.height = 18;
NSButton *myCheckBox = [[NSButton alloc] initWithFrame:frame];
[myCheckBox setButtonType:NSSwitchButton];
[myCheckBox setBezelStyle:0]; // This is unnecessary. I include it to show that checkboxes don't have a bezel style.
[myView addSubview:myCheckBox];
I don't think buttons are bezeled by default when created programmatically. Check the setBezelStyle: method, as well as setBezeled: and setBordered:. One of those should give you what you want.
I had failed to execute setImagePosition properly and this was causing the checkbox not to display.

Resources