Add a large uiview as a subview without overflowing - ios

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];

Related

is it possible to add uiview with big frame into uiview with small frame

In my app, I need to add subview in another view but my issue is that height and width of parentView is 50*50 and height of subview is 150*150 so how can I add this? because when I add this view, rest portion of subview is getting cropped.
The edges are bounded in the smaller UIView. Because the UIView is smaller it is going to crop out the larger subview. if you still want the super view to be 50x50 and don't want the larger subview cropped out then turn off setClipsToBound property of the UIView:
Swift: view.setClipsToBound = false
Objective-C: [view setClipsToBound:NO];
Another solution is just to make the subview smaller or make the superview containing the large subview larger.

How to make a UIView expand to contain a UIImageView or a UITextView with auto layout and IB?

I have a UIView that contains two subviews - a UIImageView or a UITextView. The UIImageView has a fixed height and width.
The UITextView has variable size.
Only one of the UIImageView and UITextView would be displayed at a time. I plan to accomplish this programmatically by setting hidden = YES.
I would like the UIView to hug whichever child view is not hidden with no margin.
I would like to accomplish this with IB and autolayout if possible because the rest of the view is built this way.
So far I have created constraints that link the 4 edges of both of the subviews to the parent view and constraints for the height and width of the UIImageView. Naturally this creates a content priority ambiguity.
I would appreciate any advice.
Thank you!
So the contain view has to have trailing constraint and constraint with bottom which should have IBOutlet(s) references in viewcontroller and then when the text changes :
- (void)textViewDidChange:(UITextView *)textView {
[self.textView sizeToFit];
self.containerViewTrailingConstraint.constant = self.textView.contentSize.width;
self.containerViewConstraintWithBottom.constant = self.textView.contentSize.height;
[self.view layoutIfNeeded];
}
And when the textView is hidden then you have to set the self.containerViewTrailingConstraint.constant, self.containerViewConstraintWithBottom.constant in relation with the imageView

Hide subviews when superview height change while using auto layout constraints

I have one UIView, it's having few subview controls like label's and textbox. it is also having one switch control.
I want to hide/display (like collapsible) the portion on the superview based on the switch change. However when I try to do it with constant of superview, it just changes the superview height but all subview does not getting hides.
could you please help me to figure out this.
Thanks,
Set UIView's property clipsToBounds = true.
This prevents the subviews from being drawn when their frame lies outside the bounds of the superview
e.g.
superView.clipsToBounds = true
Note that for layers you can use:
superView.layer.masksToBounds = true
There are a number of methods of UIView that allow you to modify the view hierarchy.
bringSubviewToFront:
sendSubviewToBack:
insertSubview:atIndex:
insertSubview:aboveSubview:
insertSubview:belowSubview:
exchangeSubviewAtIndex:withSubviewAtIndex:
You can use any of this as per your requirement.

UIScrollView keep scroll rect while resizing

I have got a UIScrollView with a UIImageView as subview. The contentSize of the ScrollView has been set to the size of the UIImageView.
I implemented zooming and scrolling of the UIImageView.
I need the UIScrollView to keep the scroll rect when I change its frame (proportionally).
How is this done?
My problem is, whenever I change the frame of the UIScrollView the visible rect of the UIImageView changes.
Thanks!
If I understand your problem correctly, you want to shrink the frame of the scroll view without changing what you are able to see inside of the scroll view. You can do this in the following way:
Create a new UIView and set its frame to the position and size that you want to see the content of the scroll view. Turn on the clips to bounds on that view, view.clipsToBounds = yes. Add the scroll view as a subview to the view you created. [view addSubview:scrollview]. Turn clips to bounds off on the scrollview, scrollview.clipsToBounds = no.
Hope that helps.

Subview access to superview

I have a scrollView with its content view and finally a subview on the content view.
I would like to stop scrolling [scrollView scrollEnabled:NO] from the subview. Can I get at the method?
I don't have Xcode in front of me, but can't you get at the superview with self.superview from your subview code?
For instance, like so:
[subview.superview scrollEnabled:NO];
To stop scrolling [scrollView scrollEnabled:NO] from the subview? Maybe you need a viewController which control the views. Do you clear about the MVC?

Resources