We are using a base class where we set the background picture. That code looks like:
UIImage* image = [UIImage imageNamed:#"App_Background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
CGRect bounds = self.view.bounds;
int offset = 0;
imageView.frame = CGRectMake(0, bounds.size.height - image.size.height + offset, image.size.width, image.size.height);
imageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
imageView.tag = BACKGROUND_TAG;
[self.view insertSubview:imageView atIndex:0]; // insert it behind all other views in NIB
self.background = [imageView autorelease]; // cleanup
If I want to use this as my superclass for another UIViewController but do not want to use the backgound, how do I do that? I thought I could do this in my subclass' viewDidLoad:
UIView *theBackgroundView = [self.view viewWithTag:BACKGROUND_TAG ];
theBackgroundView = nil;
But that doesn't work. Any thoughts? thanks.
Make sure you call [super viewDidLoad] first. Then do [[self.view viewWithTag: BACKGROUND_TAG] removeFromSuperview].
Related
I have a background coming from Default-568h#2x.png. I want to display a shrunk PNG over part of the background, but calling:
[[CJSHCardView alloc] initWithScale:.1];
turns the display white. This does not happen if I comment this line, but in the function call, even if I make it return immediately it turns the display white after half a second:
- (id)initWithScale:(float)scale
{
UIImage *img = [UIImage imageNamed:#"note.png"];
self = [super initWithImage:img];
if (self != nil)
{
self.frame = CGRectMake(0, 0, img.size.width * scale, img.size.height * scale);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:self.frame];
myImage.opaque = NO;
[self addSubview:myImage];
}
return self;
}
Moving/copying the return statement to be the first in initWithScale makes no discernible change from how it is with the return statement properly at the end: a brief view of the background, followed by a white screen. note.png is in "Supporting Files".
Do you see any gotchas where I can change things so that a shrunken version of the note (maintaining aspect ratio) displays? The note.jpg image does not have a pixel of white.
Thanks,
--EDIT--
Regarding the first comment:
#implementation CJSHCardView : UIImageView
- (id)initWithFrame:(CGRect)frame
{
NSAssert(NO, #"Call initWithHeight instead.");
return self;
}
Does that answer your question?
--SECOND EDIT--
Thank you for your response and your code, Nick. I have slightly altered it to fit ARC and to initially set a fixed-width scale, but I was a little unsure of what method to put it in. I tried the ViewController's viewDidLoad() method, but that resulted in the background being shown without hide or hair of the note. My present viewDidLoad() method reads:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *backgroundImage = [UIImage imageNamed:#"Default-568h"];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
float scale=.1;
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[containerView addSubview:backgroundView];
[backgroundView sizeToFit];
// [backgroundView release];
UIImage *noteImage = [UIImage imageNamed:#"note"];
CGSize noteSize = [noteImage size];
UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
[noteView setContentMode:UIViewContentModeScaleAspectFit];
[noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
[containerView addSubview:noteView];
// [noteView release];
}
Thanks,
Subclassing UIImageView is not necessary. Just create a container UIView and add 2 UIImageView subviews.
Edit - this should work for your implementation:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *backgroundImage = [UIImage imageNamed:#"Default-568h"];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[[self view] addSubview:containerView];
float scale=.1;
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[containerView addSubview:backgroundView];
[backgroundView sizeToFit];
UIImage *noteImage = [UIImage imageNamed:#"note"];
CGSize noteSize = [noteImage size];
UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
[noteView setContentMode:UIViewContentModeScaleAspectFit];
[noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
[containerView addSubview:noteView];
}
I have a few buttons that each load a different image a UIScrollView, you can zoom in on these images. What happens is that after selecting a new images the previous one is still there in the scrollview. How do I clean up the scroll view before opening a new image in there?
This is the code so far:
- (UIView *) viewForZoomingInScrollView: (UIScrollView *)scrollView{
return imgView;
}
-(IBAction)buttonClicked: (UIButton *)sender{
switch (sender.tag) {
case 1:{
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
self.imgView = myImageView;
[myImageView release];
scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 1.0;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imgView];
}
}
}
You keep adding new subviews without removing the previous ones. You could just replace the image content in the existing imageView instead of making a new one.
Replace this:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
self.imgView = myImageView;
[myImageView release];
with
self.imageView.image = [UIImage imageNamed:#"Gath.jpg"];
and remove this
[scrollView addSubview:imgView];
I assume you have linked the imageView to self.imageView elsewhere, such as in the storyboard, or when you create the scrollView.
you can try this,
[myImageView removeFromSuperView];
myImageView=nil;
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
I have a custom UIView. So myView class extend UIView. In initWithFrame:(CGRect)frame method I set UIImageView for my UIView as a background image.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.bgImage = [[[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, frame.size.width , frame.size.height)] autorelease];
int stretchableCap = 20;
UIImage *bg = [UIImage imageNamed:#"myImage.png"];
UIImage *stretchIcon = [bg
stretchableImageWithLeftCapWidth:stretchableCap topCapHeight:0];
[self.bgImage setImage:stretchIcon];
}
return self;
}
So when I create my view everything is fine.
MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
But if I want to change my view size to bigger or smaller:
customView.frame = CGRectMake(10, 10, 50, 50)];
then I have the problem with my background image. Because it's size didn't changed. What to do ? Ho to change bg image size together with view frame ?
Try setting the autoresizing mask of the UIImageView, it will resize according to the size changes of the super view.
My suggestion is use update function to update your custom view when you want to change the frame:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self UpdateViewWithFrame:frame];
}
return self;
}
- (void)UpdateViewWithFrame:(CGRect)frame
{
self.frame = frame;//set frame
if(!self.bgImage)
{
//initialize your imageview
self.bgImage = [[[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, frame.size.width , frame.size.height)] autorelease];
}
int stretchableCap = 20;
UIImage *bg = [UIImage imageNamed:#"myImage.png"];
UIImage *stretchIcon = [bg
stretchableImageWithLeftCapWidth:stretchableCap topCapHeight:0];
[self.bgImage setImage:stretchIcon];
}
When you want change frame , just call this function.Have a try man.
Set the required Autoresizingmask property attributes for the imageview correctly as it must be adjusted along with the view frames.
So the suggestions was right. I should add autoresizing mask. Now my initWithFrame looks like that:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.bgImage = [[[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, frame.size.width , frame.size.height)] autorelease];
int stretchableCap = 20;
UIImage *bg = [UIImage imageNamed:#"myImage.png"];
UIImage *stretchIcon = [bg
stretchableImageWithLeftCapWidth:stretchableCap topCapHeight:0];
[self.bgImage setImage:stretchIcon];
self.bgImage.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.autoresizesSubviews = YES;
}
return self;
}
UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
[self.navigationController.view addSubview:imageView];
I have added an image to my mainview using above code, how can I remove it from view so that it will not visible in next view?
I'm not sure that I understand the reason for doing this in the first place.
But you might set a tag value for this image view and then find it by that tag:
..
UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
imageView.tag = 100001;
[self.navigationController.view addSubview:imageView];
// Why don't you release the image view?
[imageView release];
..
UIView *imageView = [self.navigationController.view viewWithTag:100001];
[imageView removeFromSuperView];
Not sure that all the code is correct - wrote it without XCode...
Help me.
addSubview doesn't work.
I want to add "ContentView" on scrollView.
But "ContentView" doesn't appear on screen.
"ContentView" is UIView.
When I change to self.view=scrollView from [self.view addSubview:scrollView],
addSubview doesn't work.
Please teach me how to add UIView on this screen!!
- (void)viewDidLoad {
[super viewDidLoad];
scrollViewMode = ScrollViewModeNotInitialized;
mode = 1;
imageViewArray = [[NSMutableArray alloc] init];
mainStatic = [[MainStatics alloc] init];
[mainStatic setSetting];
NSString* path = [[NSBundle mainBundle] pathForResource:#"Files" ofType:#"plist"];
NSArray* dataFiles = [NSArray arrayWithContentsOfFile:path];
kNumImages = [dataFiles count];
CGRect frame = [UIScreen mainScreen].applicationFrame;
scrollView = [[touchClass alloc] initWithFrame:frame];
scrollView.delegate = self;
scrollView.maximumZoomScale = 5.0f;
scrollView.minimumZoomScale = 1.0f;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setDelegate:self];
scrollView.delaysContentTouches=NO;
scrollView.userInteractionEnabled = YES;
[scrollView setCanCancelContentTouches:NO];
NSUInteger i;
for (i=0;i<[dataFiles count];i++)
{
UIImage* image = [UIImage imageNamed:[dataFiles objectAtIndex:i]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = NO;
CGRect rect = imageView.frame;
rect.size.height = 480;
rect.size.width = 320;
imageView.frame = rect;
imageView.tag = i+1;
[imageViewArray addObject:imageView];
}
self.view = scrollView;
[self setPagingMode];
UIView* contentView;
CGRect scRect = CGRectMake(0, 436, 320, 44);
contentView = [[UIView alloc] initWithFrame:scRect];
[contentView addSubview:toolView];
[self addSubview:contentView];
}
I used image array in following function.
toolView is UIView with UIToolbar.
So I want to add toolbar to screen.
Yes, touchClass is subclass of UIScrollView.
1.I see.I forgot release this.Thank you.
2.OK. I modified this.But "ContentView" didn't apear.
are there another reason?
- (void)setPagingMode {
CGSize pageSize = [self pageSize];
NSUInteger page = 0;
for (UIView *view in imageViewArray){
[scrollView addSubview:view];
view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
}
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = scrollView.showsHorizontalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(pageSize.width * [imageViewArray count], pageSize.height);
scrollView.contentOffset = CGPointMake(pageSize.width * currentPage, 0);
scrollViewMode = ScrollViewModePaging;
}
Source isn't clear.
You never use array with UIImageView. There is no info what is toolView etc...
Please provide more detailed source. Is touchClass a subclass of UIScrollView?
What I noticed in your code:
You don't release image and imageView. All this images imageViews will be leaked. At the end of loop you should add [imageView release]; and [image release];
I don't think that it is good idea to send [self addSubview:contentView]; Try to use [self.view addSubview:contentView];