changing superview frame should not effect its subviews frame - iphone

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.

Related

UIScrollView made in storyboard has frame value of zero

I made a scrollview in my storyboard which contains severals UIImageView.
The problem is, the frame of that scrollview is equal to {0, 0, 0, 0} and I don't know why. The scrollview is visible on my screen, but I'm not able to scroll it.
I already try to set a content size, and a frame, but without success.
Fixed it out !
In fact, I develop the app on iOS 6.0 and I had to uncheck the "use autolayout" checkbox on storyboard properties...
Thanks to all anyway !
I was getting this problem too but I need to use auto layout.
I was trying to set an UIImageView as a subview then do some calcs to work out the minimum, maximum zoom scales and set the zoomScale.
The problem was that I was trying to set zoomScale to zero as the scroll views frame was zero which was giving me this error:
<Error>: CGAffineTransformInvert: singular matrix.
So I setup the image and imageView in viewDidLoad / viewWillAppear (and hide the imageView) and then set the zoomScale in viewDidAppear (then unhide imageView).
This seems to work well, although I can't say for sure why. I guess it gives the scroll view a chance to set its bounds correctly.
The issue here is AutoLayout, as some of the other answers have indicated. Specifically, the issue is that autolayout does not occur until after viewWillAppear, and if you put your code in viewDidAppear, you will get funky display artifacts as you change things on the screen while the user is watching...
If you need AutoLayout enabled, the best place to put your frame dependent code is in:
- (void)viewDidLayoutSubviews
Keep in mind that this gets called again and again though, so if you only want some initialization code run once, when the view is displayed, then you can create a BOOL property that stores whether the initial layout has already been done.
#property (assign, nonatomic) BOOL initialLayoutComplete;
And then you can write your viewDidLayoutSubviews this way:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (!self.initialLayoutComplete) {
// frame dependent code here...
}
}
As I mentioned in this answer: The frame of the scrollview will not be initialised until you are in the viewWillAppear method.
This seems to be a new behaviour in iOS 6, not sure if it's a bug or an intentional change.
You should be able to use auto layout without worrying about this.
Did you connect IBOutlet?
Did you init scrollView with this code:
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 280, 360)];
self.scrollView.contentSize = CGSizeMake(500, 360);
//values are of course only example values
How you add UIImageView to scrollView?
I think one clarification is in place to Enrico comment below.
The frame size will have the right values at viewDidAppear (and in viewWillAppear the value will be set, but they are still zero in that method)

Problem with UIScrollView inside another UIView

I have this UIScrollView inside another UIView, not occuping the entire area (all this is initialized via nib file). I added some content to the scroll view, but whenever I scroll it, the UIScrollView content area moves outside the ScrollView frame, over the UIView area not designated for displaying it.
Shouldn't it remain inside its frame even when I scroll it?
It may be that the clipsToBounds property of your UIScrollView is set to NO. Check the setting in IB, or you can set it in code like so:
scrollView.clipsToBounds = YES;
Failing that, double check that your UIScrollView has exactly the bounds you think it does. Is there any autoresizing flag stuff going on that might be changing the scroll view's bounds?
try to set the clipsToBounds to YES, it may be that:
yourViewController.clipsToBounds = YES;
if not... fbreto is right, post your code...
Make sure you have setContentSize. And also set the frame size of the uiviews inside the uiscrollview correctly when you add them.
Here is a sample code of it http://developer.apple.com/library/ios/#samplecode/Scrolling/Introduction/Intro.html

cocoa touch - view frames

my problem is the following:
I have an UIViewController initialized in a NIB. In this nib, I have created another UIView (call it view XY) and linked it to an IBOutlet inside the UIViewController.
I have then added some UIImageView area exclusively via code and added them to view XY.
This is a portion of code:
-(void) displayImage{
XYImageView=[[UIImageView alloc]init];
[imageContainer insertSubview:XYImageView atIndex:0];
[XYImageView setFrame:imageContainer.frame];
//some code that loads an UIImage into the XY view
XYImageView.contentMode = UIViewContentModeScaleAspectFit;
}
Problem is that my image appears resized correctly, BUT it is collocated outside the XYImageView area. I cannot understand why.
Also, if I try to log the view's size and origin, I get all 0's.
Maybe I am missing something?
Thank you guys.
Frame property defines view position in coordinate system of its parent, so your xyimageview gets the same coordinates as its parent has in its superview. To set child view completely fill its superview try to do the following (i.e. use bounds property, not frame):
[XYImageView setFrame:imageContainer.bounds];

UIScrollView doesn't bounce

I have a UIScrollView contained within a custom UIView with a content size larger than the ScrollView's frame.
I am able to drag scroll as I expect, but the thing doesn't give me the rubber banding effect that you get with the UITableView or UIWebView. It just stops when you get to one of the extremes.
I have set bounce = YES, is there something else I'm supposed to do?
I read the docs, and they say I have to implement the delegate. I did that.
They also say I should change the zoom levels, but I don't want the user to actually be able to zoom so I haven't set these.
For anyone that finds this thread later, if you are subclassing UIView and re-setting the UIScrollView's frame on every layoutSubviews, that is the problem - it cancels the bounce:
http://openradar.appspot.com/8045239
You should do something similar to this:
- (void)layoutSubviews;
{
[super layoutSubviews];
CGRect frame = [self calculateScrollViewFrame];
if (!CGRectEqualToRect(frame, self.scrollView.frame))
self.scrollView.frame = frame;
}
I had the same problem, on a UIScrollView that wasn't all filled up (but I still wanted it to bounce). Just setted:
scroll.alwaysBounceVertical/Horizontal = YES;
And it worked as expected
It turns out that keeping the UIScrollView within my custom UIView was causing the trouble.
Once I switched my custom UIView to instead inherit from UIScrollView, then the bouncing started working.
That is interesting... Is there a lot going on while the user scrolls the scroll view? Maybe that could cause the problem. The iPhone can multitask, but not too much. Can I see your entire code having to do with the scroll view?

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;