I don't think this would be possible but I'm hoping there's a work-around someone has found. I have a view we'll call VIEW0 and another called VIEW1.
The zPositon of VIEW0 is zPosition = 1; the VIEW1 is Position = 0 so it's below VIEW0.
In VIEW1 I have some subviews and I'd like to have one of those subviews be at a higher zPosition than VIEW0 so VIEW0's shadow will not be casted on the particular subview, but it will on all other subviews.
I have tried setting the subview's zPosition = 2 but that doesn't seem to place it above the VIEW0.
I kind of expected this because they are subviews but does anyone have any suggestions to place a subview from VIEW1 above VIEW0?
I had the same problem. I solved it by changing the parent of the subview as and when required. The subview object remains the same, but it gets a different parent & position, that's all.
[subViewObj removeFromSuperview];
and
[superViewObj addSubview:subViewObj];
Then set the position of subViewObj like this:
subViewObj.frame = CGRectMake(xStart, yStart, width, height);
Instead of changing the z order, move the subviews that you want to be above VIEW1 to be childs of VIEW1 instead of VIEW0
Since VIEW1 is above VIEW0 then all the subviews of VIEW0 will be bellow any subview of VIEW1
Related
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.
I have a UIView called containerView.
I add this as a subview to a controller view's root view. I have programmatically added a few constraints to it (I centered it and made the width a few points from the superview's width).
I have added a few UILabels to the containerView as subviews. The height of the UILabels dictate the height of the containerView.
When the user taps the screen, the containerView is moved up from CGRectOffset() and once the animation is complete, it is moved back to the original position.
CGPoint absolutePoint = self.containterView.frame.origin;
self.containerYConstraint.constant = -absolutePoint;
[UIView
animateWithDuration:0.5
animations:^
{
[viewForUpdate setNeedsUpdateConstraints];
}
completion:^(BOOL finished)
{
self.containerYConstraint.constant = 0;
[viewForUpdate setNeedsUpdateConstraints];
[self.containerView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)]; // Remove all subviews
}];
I need to remove the UILabels I have put in there as subviews and replace them with different labels. However, the moment I remove one of the UILabel's the entire containerView goes missing (I set the background as red so I can see it). I remove all the subviews in the example but when I try to just remove one the same effect occurs.
Why does this occur? Does this have something to do with auto layout? Also if I want to recenter it after I remove one of the UILabels, how do I re-do the constraints?
Modifying frames when using AutoLayout is a no-no. Once you begin using AutoLayout you're effectively telling the system that you want it to set the frames for you.
Instead of animating the frame directly, create properties pointing to your constraints and animate those constraints.
I have a view that contains a UIScrollView that is designed to scroll horizontally. (So the UIScrollView is wider than the Self.View width)
Inside the UIScrollView I have a dozen UILables that I let the user cycle through adding values programmatically, when the next UILable is programmatically selected, if it is out of bounds I change the UIScrollViews offset to make sure the UILabel appears just to the left of the right side of the view.
This is how I am currently checking the UILabel position and then adjusting the offset of the UILabel.
if (positionLabel.frame.origin.x > self.view.frame.size.width)
{
[axisContainerScrollView setContentOffset:CGPointMake(positionLabel.frame.origin.x+40 - self.view.frame.size.width, axisContainerScrollView.frame.origin.y)
animated:YES];
}
else if (positionLabel.frame.origin.x > self.view.frame.size.width)
{
axisContainerScrollView setContentOffset:CGPointMake(0.0, axisContainerScrollView.frame.origin.y)
animated:YES];
}
So positionLabel origin brings back the value of its position inside the UIScrollView then I change the offset of the axisContainerScrollView. The only problem with this is that if I scroll the view across and select a UILabel whose offset is already inside the view if send the label back across to the right..
I would like to adjust this if statement so that if the UILabel is inside the bounds of self.view then I don't want to change the offset.
You could convert the coordinate space of positionLabel.frame to be the same as self.view, then you can use CGRectContainsRect function to make the comparison you're wanting to do.
something like -
// convert label frame
CGRect comparisonFrame = [axisContainerScrollView convertRect:positionLabel.frame toView:self.view];
// check if label is contained in self.view
BOOL isContainedInView = CGRectContainsRect(self.view.frame, comparisonFrame);
Now you have a BOOL, you can place in any conditional, that you can use to check.
Have you looked at [UIScrollView scrollRectToVisible:animated:]? It may be a more elegant method to do what you want. I'm pretty sure it won't try to scroll if the rectangle is already visible.
I have a scroll view added as a sub view with a height of approx. 500.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(x,y,500,300)];
[self.view addSubView:scrollView];
Now I have another view say View1 which contains a lot of buttons arranged in a vertical direction. View1's height is approx. twice the height of my scrollView.
View1 = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,300)];
[scrollView addSubView: View1];
now I set my scrollView's content size to fit in my View1.
[scrollView setContentSize: CGSizeMake(1000,300)];
Now able to scroll through my View1 completely. Now I need to show a popUpViewController when a button within my View1 is pressed with a direction up or down depending upon the button's current location. For example if the button lies below the half way in my scrollView's frame, the popOverViewController should pop up with an upward direction.
Now how do I get relative coordinates of the button to scroll View frame, and if that button had an initial position below my scrollView's frame.height.y/2 and after scrolling the button moved up say to a position above my scrollView's frame.height.y/2, how do you get the relative coordinates then. I have tried the convertRect: toView: but no success.
I hope you guys understand what i am trying to say here.
Use the presentPopoverFromRect:inView:permittedArrowDirections:animated: method to present a UIPopoverController, anchored to (arrow points to) a particular view.
To anchor the popover to a bar button, use presentPopoverFromBarButtonItem:permittedArrowDirections:animated:.
You can let iOS automatically figure out the arrow direction.
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.