Im a beginner in programing iOS, so I have 2 questions:
1- I have a UIViewcontroller which load a few uiview and user is able to move them via touch. Now I want to put an image background in this way:
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"board.png"]];
[self.view addSubview:backgroundView];
But the image covers over all uivews!!
2- As I said, I move my uiviews by touch and when they have collided in the end touch they both goes in a folder, and a folder is created. So I want to know how I can make that a special image shows on the created folder ??
if (view != toMove && CGRectContainsPoint(view.frame,toMove.center)) {
viewToBefolder = view;
}
You can add your background view first, and then the rest of the views you add will be on top, or just do this:
[self.view sendSubviewToBack:backgroundView];
If your image view is covering the the background, you should be setting the size and position of the image view, or setting the constraints so that it is added at the size and position that you want.
Set your imageview into the main window.
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[self.window addSubview:backgroundView];
[self.window sendSubviewToBack:backgroundView];
Then set all your view controller view clear color. so, By default it's always display background image view into the all view.
Related
I have set up a launch image and this shows correctly when the app starts. In the initial view controller I do this in the viewDidLoad:
[self.imageBackground setImage:[UIImage imageNamed:#"Default.png"]];
Default.png is the same image as the launch image. Why does the image snaps (the image gets resized in the initial view controller) and how can I prevent this?
Thanks in advance
Try this.
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default.png"]];
[self.view addSubview:backgroundView];
Add backgroundView.frame if you are facing any problem.
Its is based on your image view size in view controller. lets set image view size same as view size in view controller. It will reflect the same size.
I have a really large image that I want to use as a background image of a view. However, I don't want to display the entire image at once; I want only a part of the image to be displayed, and then I want to animate it to display other parts of it, similar to the "infinite background" in games (only not infinite in my case ;)).
What is the best way to do this? Will I have to separate the image in several pieces and then somehow animate the transition between the pieces, or is there a better way?
How about having UIScrollView as a background view? You can then put UIImageView inside that scroll view and control scroll view's contentOffset as needed.
I found the solution. This piece of code does the magic:
self.backgroundImageView = [[UIImageView alloc] initWithFrame: self.view.bounds];
self.backgroundImageView.image = [UIImage imageNamed:#"background.png"];
self.backgroundImageView.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview: self.backgroundImageView];
The key was setting the contentMode to UIViewContentModeBottomLeft.
I'm trying to create an interface like this
Where I have a piece of torn paper with drop shadow that sits below the nav bar but above my tableview.
I use the following code in my tableview controller
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed: #"ripped-paper"]]];
}
This works fine except the ripper paper scrolls with the table view. I require it to stay fixed under the navbar.
Any ideas?
In iOS 6, you can just use the shadowImage property of UINavigationBar.
UIImage *image = [[image imageNamed:#"tornPaper"] resizableImageWithCapInsets:UIEdgeInsetsMake(/* Your insets here */)];
self.navigationItem.navigationBar.shadowImage = image;
You could try and add your image to the table view controller self.view.superview:
[self.view.superview addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed: #"ripped-paper"]]];
You should execute this in viewDidAppear, though (otherwise self.view.superview will not be set yet).
This could require also changing the frame/center, more or less like this:
UIImageView* rippedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"ripped-paper"]];
rippedView.center = <SET CENTER HERE>;
[self.view.superview addSubview:rippedView];
But in the end it will greatly depend on your view hierarchy.
EDIT:
for your autorotation issue, try to set the view autoresizingMaks
rippedView.autoresizingMaks = UIViewAutoresizingNone;
and see if things improve. That way, the image view should not be resized on rotation. (Also: are you doing anything in your rotation method?)
I have a XIB file with UIControl and UIScrollView elements inside of it. I would like to add a background image to the view. I tried adding an ImageView in IB but I could not get it to be present as a background and it obscured the control elements. Sending a sendViewBack message doesn't seem to do anything either.
When I create a UIImageView programmatically, it doesn't show up.
Below is the code I attempted:
Programmatic Creation
UIImage *imageBackground = [UIImage imageWithContentsOfFile:#"globalbackground"];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:imageBackground];
[[self view] addSubview:backgroundView];
[[self view] sendSubviewToBack:backgroundView];
Dealing with the NIB file
[[self view] sendSubviewToBack:background];
where background is an IBOutlet declared in the header file and connected to the NIB's image view in IB.
Is there a step I'm missing here?
Set the frame and dont use sendSubviewToBack:. If you are working with UIImageViews you have to use [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"imageName.png"]];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"imageBackground"]];
backgroundView.frame = self.view.bounds;
[[self view] addSubview:backgroundView];
hope this was the deal.
Don't add the image view as a subview of the scroll view, it needs to be a separate view at the top level of the hierarchy, then sent to the back of the Z-order.
You will need to set the background of your scroll view to [UIColor clearColor], and ensure that the scroll view is not marked as opaque. You can do this in code or in interface builder.
Don't use imageWithContentsOfFile and then just pass it a filename with no extension (I'm assuming .png) - this is probably returning nil. Use imageNamed: instead (you don't supply an extension in that case, iOS4 and above)
Depending on the nature of your image, you can also generate a colour with it and use that as the background colour of your scroll view. I'm assuming self.view is the scroll view:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"globalBackground"]];
I designed a very simple interface for an ipad device: UIView + a navigation bar.
Then after the view is load, it will download an image from a location and use the following method to display it:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
UIImage* testImg = [UIImage imageWithData:_networkData];
UIImageView* testView = [[UIImageView alloc] initWithImage:testImg];
[_view addSubview:testView];
[testView release];
}
The problem is now the new UIImage occupies the whole visible area of the Ipad.
I wonder how can I fix this issue? I suppose I have to find out the frame of the display area in the original view, and then assign it to the UIImageView instance?
initWithImage will automatically adjust the frame to match the size of the image you're passing in. To tell the UIImageView to take the same size as its parent view you could just add the following line before you add the subview:
testView.frame = _view.bounds
...we use bounds rather than frame because the superview may have an offset that we don't want the image view to have.