UIView subclass won't autoresize - iphone

I've been looking for background information on resizing, but I haven't been able to find much. I know I need to set autoresizesSubviews on the superview and autoresizingMask on the subview.
I have done this, and my UIImageViews properly resize, but my custom UIView subclass does not.
The code for the UIImageView:
UIImageView *newX = [[[UIImageView alloc] initWithImage:dot] autorelease];
[newX setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[newX setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 1)];
[self.view addSubview:newX];
The code for the custom UIView subclass:
trace = [[TraceView alloc] initWithFrame:self.view.bounds];
[trace setAutoresizingMask:(UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin)];
[self.view addSubview:trace];
From what I can tell, the view rotates as expected, but does not resize. I can no longer see the bottom of it, and it doesn't fill the screen to the right.
Do I have to add anything to the UIView subclass to make resizing work properly?
EDIT:
I've changed the autoresizingMake to (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) and called [subview setNeedsDisplay]; I'm not sure why this is necessary as the UIImageViews work fine without it, but it now behaves as expected.

I think you're missing UIViewAutoresizingFlexibleHeight or width on the subview. Otherwise I think the view just moves around according to the margins but doesn't resize.

You mention rotation. Is the superview that contains your custom view actually resizing correctly? If in doubt, set its background to some odd color. If it did not resize correctly after the orientation change then you should see a white or grey bar on the right.

Related

changing superview frame should not effect its subviews frame

there
I have a viewController ,the hierarchy is something like this
UIView->imageview->uiview->uiimageView->uitableview.
Now i want to change height of only first two object of my hierarchy i.e UIView and UIImageView without effecting frames of other subviews.Please help me.
According to your original question, this will work for you.
self.view.autoResizeSubviews = NO;
self.myImageView.autoResizeSubviews = NO;
and then resize.

UINavigationController - Keep Background Image Across All Views

Is there a way to have a background image remain constant across all views in a navigation controller? Currently I am loading the same background in each view's viewDidLoad method but this shows the background image move when navigating from view to view. I'd rather just the content of the view infront of the background "slide" on/off screen, but the background stay stationary. This is my current background image loading code:
UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
background.image = [UIImage imageNamed:#"InfoBackground.png"];
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
[background release];
Thanks!
Hm, perhaps if you look at the documentation (scroll down to Figure 2) you will get an idea of what you're dealing with. Because you are setting the background image for each of your view controllers that are being pushed into the UINavigationController, you will get that animation. What you need to do is set the background image into the nav controller itself.
I believe myNavController.view insertSubview:myImageView atIndex:0 should work. If your image needs to fill in behind the content view exactly, you could set the frame coordinates based on the coordinates and/or heights of the navbar and toolbar, which can be accessed through the navigation controller's properties. If not, just set the frame to the superview's bounds.
Let me know how it goes.
Edit: Oh, note that you would need to make sure each of your view controllers had transparent backgrounds.
i think the better idea is place background image on window and set all view's(all viewcontroller's view) background color to clear color [UIColor clearColor].
if you want background image static then there is only one way but i don't know that is possible or not, If we put image in window and make navigation controller transparent then it's stay static whatever you will do. because we are not changing window while push or pop.
I am just suggesting try this way i haven't tried like this.

Setting a jpg as the background for a UIView?

I know this is a very beginner question, but I'm obviously a beginner. I have already made a my TabBar but I want to set the background of one of the views as a (jpg) I created. I need to add the background in code (not IB) so that I can allow rotation and resizing when the iphone is rotated.
-thanks
You need to use a UIImageView, which is a subclass of UIView. You can create it as follows:
UIImageView *myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.jpg"]];
[self.view addSubview:myImage];
[myImage release];
...so here I've created a UIImageView that uses a JPG called 'myImage' (it will automatically resize the view to fit the image), added it to my view controller, and then cleaned up my memory.
You can try this one to set an image as a background for a view programmatically:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]]
Add an instance of UIImageView to your view, and set it's image to an instance of UIImage created using your file.
You can add the image view in IB or in code -- there's nothing about doing it in IB that would prevent you from resizing, rotating, etc.

Auto resize subviews on device rotation

I have all my subviews set up so that they are based on self.view.
EG: UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,(self.view.frame.size.width-20),(self.view.frame.size.height-90))];
however when the view rotates (shouldRotateToDeviceOrientation or whatever) the views all stay the same size. How can I make them change shape to fit? Can I do this automatically?
Thanks
Absolutely. Take a look at the autoresizingMask property. If you set your image view, in this case, to have an autoresizing mask of UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight, then when its parent view resizes (as it may or may not automatically do when your app rotates—you might have to set a similar autoresizingMask on the parent view), it'll maintain the exterior margins you set up for it.
Just in case you have a view in the bottom of parent view this should help:
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
Where self is your child view, that should be resized. If you do not set UIViewAutoresizingFlexibleTopMargin, then in horizontal mode you will not see your view, if it was in the bottom in vertical mode.
Also do not forget to set:
self.autoresizesSubviews = YES;
And autoresizing modes for all child view in your view. So that when it is resized, everything inside it also gets resized.
you have to [view setAutoresizingMask:...]

Get correct bounds for navigationItem.titleView layoutSubviews

I have a subclass of UIView that I've added to as the titleView of a navigationItem using the following line of code:
self.navigationItem.titleView = tempview;
Easy enough. That works fine. My problem is that this navigationItem sometimes has the rightBarButton updated (sometimes there is no button, sometimes there is one standard sized button, sometimes there is a larger button).
I figured that I could simply use the layoutSubviews method of the tempview class that I've added as the titleView so I put this in:
-(void)layoutSubviews {
[super layoutSubviews];
self.mylabel.frame = self.bounds;
}
This does not seem to work, as it does not correctly resize the titleview when the rightBarButton item is updated.
I've noticed also that the bounds do not grow once they gotten smaller, they simply change the position.
I've tried using setNeedsLayout and layoutIfNeeded but those simply "resize" the view with the incorrect bounds.
I've also made sure that the rightBarButton item is set to nil, but the view still does not correctly expand once shrunk.
Thanks for any help!
configure your view with
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
(probably in the initWithFrame)
then implement
- (void)willMoveToSuperview:(UIView *)newSuperview
{
[self setFrame:[newSuperview bounds]];
}
now you have your view matching the size of the container, and resizing automatically.
By default, layoutSubviews does nothing. You also never change the size of the titleView, so unless a navbar does that for you, it's no surprise that nothing is changing.
Since you're only changing the size of one of the subviews, I don't think autoresize will work, as I'm pretty sure it's triggered when the superview (the one with autoresizesubviews enabled) changes size. I would suggest recalculating the size of the titleview when you change the rightbutton. If it automatically fits when you add it the first time, you could remove and readd it, but I know that's a pretty ugly hack.
Instead of overriding -layoutSubviews have you tried setting the autoresizingMask property?
tempView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;