ScrollView offsets when superview is setframe?! (iphone) - iphone

So i have a View and a scroll view inside that view. I call a
[View setFrame:CGRectMake(55,70,260,420)];
i put a
NSLog(#"%f %f",scrollview.frame.origin.x, scrollview.frame.origin.y);
before and after the setFrame and it reads 6,112 and then 6,172. 112 is correct, i have no idea where the 60 comes from. i searched 60 in my implementation file, and there is nothing that could affect the y at all. is there some rule with doing a setframe when there is a view inside that view?
Thanks

If you take a look at the UIView reference, the frame property states
frame
The frame rectangle, which describes the view’s location and size in
its superview’s coordinate system.
#property(nonatomic) CGRect frame
Discussion
This rectangle defines the size and position of the view in its
superview’s coordinate system. You use this rectangle during layout
operations to size and position the view. Setting this property
changes the point specified by the center property and the size in the
bounds rectangle accordingly. The coordinates of the frame rectangle
are always specified in points.
So i guess when you NSLog the frame for scrollview it is displayed based on the coordinates of the superview. The second NSLog shows different because the superview's coordinate system has changed.

I fixed this by making the scrollview in my code instead of doing it through the XIB. the scrollview is created after the setFrame and addSubView of the parent View

Related

iOS How to get the content offset of a view inside a scrollview

Lets say I add 10 views to a scrollview. I obviously give them a rect inside of the scrollview. Is there any way to figure out what each item's offset is in the scrollview?
I'm guessing that probably isn't applicable here. What I need in essence is, "If this item is at this content offset in the scrollview (meaning visible to the user in a certain area of the scrollview), then do this".
Each view's frame is expressed in its superview coordinate system (bounds). That is, in your case for each view its content offset is CGRectGetMinY(view.frame).
You might want to find the current scroll position by looking at UIScrollView contentOffset
The point at which the origin of the content view is offset from the origin of the scroll view.
With the contentOffset, you can then compare this with the frame x and y coordinates of the content views and determine if they are onscreen or not.

How do I position a subview within a principal view?

I created a class in my project and this class is a subclass of another class; I want place this subview in the principal view, I reduced x and y to make it smaller, but it always appears at the top left, how can I set it in other position?
Would be easier to answer if you posted some code. Most likely, you need to modify the frame property of your view. frame is a rectangle (CGRect) that determines the position and size of your view in the coordinate system of its superview. The bounds property is similar but expresses origin and size in the coord system of itself (e.g. origin is 0,0).
For other layout tips, look at the autoresizingMask and contentMode properties of views. In fact, read up on views in general. The Apple docs are good and lots of stuff here about them.

how do i get a layer's frame to automatically resize based on its superlayer's frame or its view's frame?

I'm experimenting with using cagradientlayer to draw gradients in our app instead of having a subclass of uiview manage gradients. One snafu that i've come across is that when the view that has the gradient as a sublayer of its main layer gets resized to fit the data i am trying to show, the layer doesn't resize along with it. I end up having the gradient layer end at the original frame size while my view's frame is much larger.
Is there a way to have the sublayer autoresize to fit its superlayer's frame, or the superlayer's view's frame?
Implement layoutSubviews in the subclass of your view. It gets called when the frame is resized (or when setNeedsLayout is called). Just set the layer's frame to the view's bounds:
-(void)layoutSubviews
{
someSubview.frame = self.bounds; // make the subview frame match its view
}
This will work even better
-(void)viewWillLayoutSubviews

Current location of the object inside UIScrollView

I created UIScrollView and added large image inside of the scrollview to scroll. My question is how should I know the current location of the image that I added inside of the scrollview like its X and Y? Like for example, I scroll the image horizontally then and I want to know what its new X and Y value of the image.
This is going to take a little bit of math. There are two properties (both CGPoints) that are of importance to you:
The frame.origin of your image, which is its position within its superview
The contentOffset of the scroll view, which is the visible portion of the scroll view
Basically what you want to do is grab the frame.origin.x and frame.origin.y properties of the image view, which will tell you where the image is relative to the absolute origin of the scroll view. Then, get the contentOffset.x and contentOffset.y of the scroll view and subtract them from the x and y of the image view. That should provide you the relative origin of the image view to your screen.
See scrollview.contentOffset. That should have the coordinates of the point that is visible in the top-left corner of the scrollview after any scrolling.

Why does my view move around, when I change its center?

I thoght that it's frame's oringin decides the position of the view. But when I change the center property like so
myView.center = CGPointMake(myView.center.x - 20, myView.center.y);
my view will move 20 units to left. I want to change the center point to make some rotations relative to that point.
If you want to change the rotation point, set the anchorPoint instead. The anchorPoint is on the layer in the view, but the rotation applied to the view still uses the anchor point.
From the Apple API for UIView:
The center is specified within the
coordinate system of its superview.
Setting this property changes the
values of the frame properties
accordingly.
Changing the frame rectangle
automatically redisplay the receiver
without invoking the drawRect: method.
If you want the drawRect: method
invoked when the frame rectangle
changes, set the contentMode property
to UIViewContentModeRedraw.
So essentially, changing either center, frame, or bounds will reposition and resize the view appropriately, changing the other two properties accordingly.