I wanted to add a background image to my UIViewcontroller in my iOS app.
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[imageView setImage:[UIImage imageNamed:#"background"]];
[self.view addSubview:imageView];
}
This unfortunately produced a result where the whole screen was covered.
Is there a way to move this subview to the bottom layer so that it doesn't cover my buttons and textfields?
You need to insert the view at the bottom of the hierarchy
[self.view insertSubview:imageView atIndex:0];
Related
I have created a scrollview programmatically in ios.
I am displaying 2-3 images on that scrollview and adding images vertically one below other.
When I touch on the image and scroll vertically it works. But if I touch the empty space between the images and try to scroll it does not work. Scrollview does not respond to that.
Here is the relevant code :
UIImageView* imageView = [[UIImageView alloc] initWithFrame:imageRect];
[imageView setImage:[UIImage imageNamed:#"background.png"]];
[scrollView addSubview:imageView];
[self didAddSubview:imageView];
.....
......
[scrollView setScrollEnabled:YES];
[[self view] addSubview:scrollView];
Any inputs will be helpful.
Thanks.
You can set the content size of scrollView use this code hope it will help you :-
- (void) viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
[scrollView setScrollEnabled:YES];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 250, 200)];
[imageView setImage:[UIImage imageNamed:#"background.png"]];
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
scrollView.contentSize = CGSizeMake(320, 600);
}
Set,
[scrollView setFrame:size];
[scrollView setContentSize:contentsize];
If it will not scroll vertically or horizontally then you have to change content size it should be greater then current size.
This question is old but I was having the exact same issue with my scrollview, and this was the only page I found about this all over the internet.
I was using a content view inside scroll view, and I just set a background color on my contentView and it worked.
Content size was being set automatically throw auto layout, so no need to set it manually.
I'm developing a iOs app for iPad and I'm implementing a UIImageView inside a UIView. The problem is that if the position of the image view has a position out of the UIView that it is inside, you can see it. The code:
UIView *vistafunda = [[UIView alloc] initWithFrame:CGRectMake(512/2, 48, 525, 651)];
[self.view addSubview:vistafunda];
vistafunda.backgroundColor = [UIColor darkGrayColor];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hola.png"]];
img.center = CGPointMake(12, 200);
[vistafunda addSubview:img];
I would like that the image view was always under the view, so if the image is outside the view you could not see it.
Thanks!
Just make the containing view clip it's children
vistafunda.clipsToBounds = YES;
I am trying to place a UIImageView behind a the tableView in a view controller that is a subclass of UITableViewController. I have gotten the following code to sort of work, but the image scrolls with the rows in the table.
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[backgroundView setImage:[UIImage imageNamed:#"woodbackground1"]];
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
[self.tableView setBackgroundColor:[UIColor clearColor]];
I tried the suggestions in this post
Add UIView behind UITableView in UITableViewController code but they produce the same result, am image that across with the table.
Is there a way to place the image view behind the tableview so that the image does not move with the table?
Thanks,
Dan
Why not use
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background"]];
I have a subview in my app that contains a UIImageView. The user can touch this subview and move it around. I have some UILabels that I want to "stick" or group with the subview so that they move along with it. I wonder how to do that?
Here is where I add the Labels to the subview:
- (void)viewDidLoad
{
[super viewDidLoad];
//Decrease the site of root CALayer of the view and give it round corners
self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
self.view.layer.cornerRadius = 10.0;
self.view.layer.masksToBounds = YES;
self.view.layer.frame = CGRectInset(self.view.layer.frame, 40, 40);
//check if the background is from the photo library and create a sublayer for the semi transparent white badge and a textsublayer for the countdown labels. Also hide the regular view labels
NSString *path = [_occasion imagePath];
if (path != nil && [path hasPrefix:#"ass"])
{
//Make a subview with a UIImageView inside for the badge
CGRect badgeRect = CGRectMake(32, 13, 236, 222);
UIView *badge = [[UIView alloc] initWithFrame:badgeRect];
imageView = [[UIImageView alloc]initWithFrame:[badge frame]];
[imageView setImage:[UIImage imageNamed:#"badge.png"]];
[badge addSubview:imageView];
[self.view addSubview:badge];
/*[self.view bringSubviewToFront:countDownLabel];
[self.view bringSubviewToFront:daysLabel];
[self.view bringSubviewToFront:hoursLabel];
[self.view bringSubviewToFront:minutesLabel];
[self.view bringSubviewToFront:secondsLabel];
[self.view bringSubviewToFront:daysName];
[self.view bringSubviewToFront:hoursName];
[self.view bringSubviewToFront:minutesName];
[self.view bringSubviewToFront:secondsName];*/
[badge addSubview:countDownLabel];
[badge addSubview:daysLabel];
[badge addSubview:hoursLabel];
[badge addSubview:minutesLabel];
[badge addSubview:secondsLabel];
[badge addSubview:daysName];
[badge addSubview:hoursName];
[badge addSubview:minutesName];
[badge addSubview:secondsName];
//more code for recognizing the touches on the subview
Just put them on the UIImageView and they'll move along with it.
You can get the position of your subview and change the position of you labels according to that.
The following snippet shows up on the screen on top of correct background, however text does not scroll.
What am i missing please?
-(void) viewWillAppear:(BOOL)animated {
UITextView *scrollableText = [[UITextView alloc] initWithFrame:CGRectMake(20, 20, 300, 800)];
[scrollableText setOpaque:TRUE];
[scrollableText setBackgroundColor:[UIColor clearColor]];
[scrollableText setEditable:NO];
[scrollableText setText:#"some really really long text"];
[scrollableText setScrollEnabled:YES];
UIImage *backgroundImage = [[UIImage alloc] initWithCGImage:[UIImage imageNamed:#"about_bg.png"].CGImage];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[backgroundView addSubview:scrollableText];
[[self view] addSubview:backgroundView];
}
Try [textview setUserInteractionEnabled:YES] or textView.userInteractionEnabled = YES; in addition to setScrollEnabled.
i have the same problem and i resolve in this mode.
Put in your view a customView like OBGradientView and in this add your textView.
in the code add at view your ObGradientView and at it add textview.
work 100% trust to me
here the link for subview