UIImageView not showing on iOS6 - ios

in ios5 i implemented my UIImageView like this:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"FirstScreen.png"]];
imageView.frame = CGRectMake(0.0,0.0,[GeneralUtils screenWidth],[GeneralUtils screenHeight]);
// !!!!!!!!!!!!!
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
[self.view sendSubviewToBack:self.tableView];
Everything´s fine...But now in ios6 my UIImageView is not showing anymore....

try bellow code..
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"FirstScreen.png"]];
imageView.frame = screenBounds;
[self.view addSubview:imageView];
[self.view bringSubviewToFront:self.tableView];
[self.tableView setBackgroundColor:[UIColor clearColor]];

Solved it by setting
[self.tableView setBackgroundView: nil];
I think my tableView was hiding the Image!

Related

Unable to set Background only for UiImageView only

Im trying to give a background for an image view only as white. Following is my code.
dataArray = [[NSMutableArray alloc] init];
[dataArray addObject:#"Knowledge"];
[dataArray addObject:#"Client"];
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float pickerWidth = screenWidth * 3 / 4;
float xPoint = screenWidth / 2 - pickerWidth / 2;
pickerView = [[UIPickerView alloc] init];
pickerView.transform = CGAffineTransformMakeScale(1, 1);
[pickerView setDataSource: self];
[pickerView setDelegate: self];
[pickerView setFrame: CGRectMake(xPoint, 200.0f, pickerWidth, 200.0f)];
pickerView.showsSelectionIndicator = YES;
[pickerView selectRow:2 inComponent:0 animated:YES];
UIImageView *imageview = [[UIImageView alloc]
initWithFrame:CGRectMake(30, 20, 300, 400)];
[imageview setImage:[UIImage imageNamed:#"logo.png"]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
[imageview setBackgroundColor: [UIColor whiteColor]];
[self.view addSubview:imageview];
[self.view addSubview: pickerView];
Im facing two problems while doing so,
The background colour is being applied for both UIImageView and UiPickerView thought I have specified specifically as [imageview setBackgroundColor: [UIColor whiteColor]]; when it has to be only for UiImage.
The alignment changes when the phone is in landscape mode though I have given a proper coordinates. How can I sort this out?
Set pickerview's background color like,
pickerView.backgroundColor = [UIColor lightGrayColor]; //desired color
hope this will help :)
Try this code:
imageView.backgroundColor = [UIColor clearColor];
imageView.opaque = NO;
Try this :-
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background"]];
[self.view addSubview:backgroundView];

Scroll Doesn't work Help would be graet

I have tried this code but scrollview is not working.
Please help
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
scrollView.showsVerticalScrollIndicator=YES;
CGRect contentRect = CGRectZero;
for (UIView *view in scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
scrollView.contentSize = contentRect.size;
//scrollView.contentSize=CGSizeMake(self.view.bounds.size.width,tableView.bounds.size.height+100);
[self.view addSubview:scrollView];
UITableView *tableView=[[UiTableView alloc]initWithFrame:CGRectMake(0,200, self.view.bounds.size.width, self.view.bounds.size.height)];
tableView.dataSource=self;
tableView.delegate=self;
//tableView.keepOneSectionOpen = YES;
//tableView.initialOpenSections = [NSSet setWithObjects:#(0), nil];
tableView.scrollEnabled = NO;
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellReuseIdentifier];
[tableView registerNib:[UINib nibWithNibName:#"AccordionHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:kAccordionHeaderViewReuseIdentifier];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,200)];
imageView.image=[UIImage imageNamed:#"download (1).jpeg"];
imageView.userInteractionEnabled=YES;
imageView.alpha=1;
[scrollView addSubview:imageView];
[scrollView addSubview:tableView];
UITableView is a subclass of UIScrollView, not a subview, so you should just use the table view directly.
For more information read this explanation
I think the way you did is not correct. If you want to add an image above table view, try using tableHeaderView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
self.tableView.tableHeaderView = imageView;

How to set specific size to UIImageView

I got a weird bug going on. I'm trying to programatically initialize an UIImageView with a specific frame and image. My image is 60x60, but I'm trying to resize it to 30x30 on the screen, which seems impossible.
The following works, but show the image in 60x60:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
// imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];
And the following just doesn't show anything at all on the screen:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];
What's wrong?
EDIT: for those having the same problem, here is the working code:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
imgView.frame = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[imgView setContentMode:UIViewContentModeScaleAspectFill];
[self.view addSubview:imgView];
Start experiment with
imgView.center = CGPointMake(view.bounds.x/2, view.bounds.y/2);
imgView.bounds = view.bounds;
and see if this fills the superview. Then try CGPointMake(pointTouch.x, pointTouch.y) and CGPointMake(30,30) respectively.
I'm guessing this behaviour has something to do with warning about frame:
If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
You could try initializing the UIImageView with a set frame, then setting the image after that:
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 30, 30)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
imgView.image = [UIImage imageNamed:#"logo.png"];
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];

What is wrong with the last line of code here?

Am trying to set up a view hierarchy in the viewDidLoad method, and am getting a problem with the last line.
UIView *catView = [[UIView alloc] init];
UIImage *image = [UIImage imageNamed:#"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
[self.view addSubview:imageView];
It looks like you want to change
[self.view addSubview:imageView];
to
[catView addSubview:imageView];
See https://teamtreehouse.com/forum/programming-a-background-stuck-on-code-challenge

Gap is existing when customize the navigationbar with imageView

Im using the following codes to customize the navigationbar with an UIView. But why there is a gap in the left size of the navigationbar? ps. the gradient image is test.png.
Thanks in advance!
- (void)viewDidLoad
{
[super viewDidLoad];
// show image
UIImage *image = [UIImage imageNamed: #"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.frame = CGRectMake(0, 0, 320, 44);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
tmpTitleLabel.text = #"title";
tmpTitleLabel.textAlignment = UITextAlignmentCenter;
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 320, 44);
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
}
Edit 1
Here is my test code and screen shot
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * viewGreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView * viewRed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewGreen setBackgroundColor:[UIColor greenColor]];
[viewRed setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = viewRed;
[self.view addSubview:viewGreen];
}
your code is work fine.just check the image and transparency.
To add the line at last and check again
[self.view addSubview:newView];
problem is solved by the following code
[self.navigationBar insertSubview:imageView atIndex:1];

Resources