Get view's frame in UIView that is not superview - ios

I would think this is a simple question, but I cannot seem to find a way to do this.
I have a view that is a subview of my uiviewcontroller. Within that view, I have another view. To clarify, this is the architecture:
>UIViewController.view
--->Subview A
------->Subview B
I want to get the frame of subview b within uiviewcontroller.view.
Is this possible? subviewB.frame gives me the frame of the view within Subview A.

Use the UIView convertRect:toView: method:
CGRect frameRelativeToViewControllerView = [subviewB convertRect:subviewB.bounds toView:viewController.view];

Related

iOS:use the frame constrained by Auto Layout to create a subview

I create a view called boundView using IB and Auto Layout,then in the controller I call [self.boundView layoutIfNeeded],then I pass self.boundView.frame.size to a method to generate the size of boundView's subview CardView. And then use the
PlayingCardView *playingCardView = [[PlayingCardView alloc]initWithFrame:frame];
to create subview programmatically. I do use NSLog to check that the size of subview is smaller than superview. But when I use [self.boundView addSubview:CardView] to add the subview. It is larger than superview!
Is there something wrong with the coordinate?Or it is because I combine the Auto Layout with the view I create by code?
Where are you doing this? If it is in viewDidLoad then the autolayout sizes will not have been calculated yet. Try doing it in viewDidLayoutSubviews.

Is there a way to get a collectionViews frame from a collectionviewcell

I want to use
-preferredLayoutAttributesFittingAttributes inside the UICollectionviewCell subclass we are writing.
Currently we already had a weak pointer to out parentViewController and I try to set the collectionViewcell frame width to be the same as parentViewcontroller.view.frame.size.width.
I don't want to have a pointer to the parentViewController from the CollectionViewCell subclass as having a reference to the parentviewcontroller is a bad idea.
My question is....
Is there a simple and ideal way to get the frame of collectionviewcell's super view from inside the collectionViewcell class?
If you want to be use the parent view then you don't need any reference. You can do like this:
CGFloat width = self.superview.frame.size.width;
And as #Ralfonso pointed out in the comments:
Also, if you want to get the superview size immediately after being added to a view hierarchy, override didMoveToSuperview: and access the superview property. If it's nil, the view has been removed from the superview. If non-nil, the view has been added to the superview's hierarchy.

iOS Storyboard: how do you edit a content view larger than screen in a UIScrollView

I have a view that is larger than screen, I need to put it in a UIScrollView.
So I first add an UIViewController to story board, then, I add a UIScrollView to the root view of my view controller, then when I add subviews of UIScrollView, but I can't add them outside the scrollview area I can see, how to solve this problem?
Change the simulated size of the view controller to Freeform and then you can set the size to whatever you want. After editing, you may want to set it back.
Edit 1:
1) put your subview into scrollview's visible area
2) change frame of the new subview as you wish
3) change contentSize property of scrollview with runtime attributes
Edit 2:
u can create a new view with xib, which contains all your subviews, and then add this view on scrollview OR u can use storyboard's Container View like this:
p.s.: don't forget about contentSize (point 3 in my first edit), but if you are using auto-layout, you need to set it programmatically like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentSize = CGSizeMake(320, 250);
}
put a scroll view in the view controller ,and put a view inside the scroll view named containerView.
design the big content in another view controller (named bigContentView)
in code ,view load event ,
a. get the bigContentView by storyboard Id. ( get the controller and use controller.view)
b. create the outlet to the containerView.
c. update the containerView's frame according your requirement.(update the width and height)
d. bigContentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
e. containerView addSubView:bigContentView.

Add a large uiview as a subview without overflowing

I have a uiview that i need to add a subview to, the subview's width
is twice the width of the parent view, and i don't want this suview to overflow,
i need the overflown area to be hided, how can i achieve that?
You can use this:
view.clipsToBounds = YES;
For your superview - the view which has the subview, call message
[*SUPERVIEW* setClipsToBounds:YES];

How to set a subview's frame

I have a view hierarchy under a UIViewController that consists of a main view and a few subviews of the main view, all defined in a storyboard, and all connected using IBOutlet.
I would like to set the position and size of the subviews relative to the main view.
I've tried setting them in viewDidLoad and viewWillAppear. Neither work, and when called, the main view has valid frame values but the subviews all have frames of (0,0,0,0).
If I do nothing, the subviews appear exactly where positioned on the storyboard. I need to make the following code work, but I don't know where to put it.
CGRect newFrame = CGRectMake(10,10,50,50);
[subView setFrame:newFrame];
Another way to ask this question:
Where and when do the frames of storyboard subviews get set?
Or, should I just create all the subviews with code?
when using autolayout you can set frame of the views (if the frame is calculated from super view's frame) in viewDidLayoutSubviews method or the other way I use is calling this method [view setTranslatesAutoresizingMaskIntoConstraints:YES]; and then [view setFrame:...];. Hope this helps.
The position and size of subviews can be set when the UIViewController calls viewDidAppear but that causes the presentation to jump. Setting the position and size of a UITableView in viewDid/WillLayoutSubviews does not work.

Resources