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"]];
Related
I have UIViewController which set top storyboard and having few subviews in storyboard. and I want to add a UIImageView programmatically and set it as a background view. so if I add a UIImageView, storyboard subviews are not visible.
how can I send this UIImageView behind all the storyboard subviews?
Thanks
You need to go with the sendSubviewToBack:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.view.frame];
imgView.image = [UIImage imageNamed:#"blank-background.jpg"];
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
use method of subview's "bringSubViewToFront".
like
[parentView bringSubViewToFront:subViewImg];
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.
I have written some code which applies and image to a view for a UIViewController. The code is supposed to be iPhone screensize independent in as far as the difference in height between the iPhone 4 and 5.
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.clipsToBounds = YES;
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"myimage.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor colorWithPatternImage:image];
[self.view addSubview: imageView];
image = nil;
imageView = nil;
I found that when I add this code to the viewDidLoad method, the code failed to detect the different window size. However when I place it in viewWillAppear, the code does correct work with both screen sizes. I don't understand why.
Does any one know why this would happen ? I would like to understand it.
thanks
This happens because at the time the view is loaded, its' content hasn't necessarily been laid out and the size isn't known. This is especially true when using the autolayout system. The basic steps are,
The view is loaded
The view is laid out by the system using the constraints you give in the storyboard or code
The view appears
So the most appropriate place to put this appears to be viewDidLayoutSubviews. At that point the view and its subviews have been laid out, and the sizes are there. But putting it in viewWillAppear (or viewDidAppear, for that matter) will work, albeit will be less correct.
viewDidLoad method is called when the view controller's root view just has been created. At this point it is not added to the window hierarchy and auto layout has not been done. That is why you see this behavior.
This being said why draw the image in the graphic context? Just use UIImageView to display the image.
What you are doing is almost correct. As others have stated, in viewDidLoad the final size of the view controller's view is not set yet. The proper solution is to set the subview's autoresizingMask properly.
UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor colorWithPatternImage:image];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview: imageView];
This assumes you want the image view to fill the view controller's view.
Another solution is to update the frames of the subviews in the viewWillLayoutSubviews method.
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 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.