I am working on a project. I have a UIView that has several subviews. I need to know how to mask only certain subviews to the parent view, or mask all but one particular view. Is there some way to add refinement checks to the maskToBounds property? Any direction or suggestion would be greatly appreciated.
What do you mean by masking?
If you want to hide particular view, you could create a standalone additional view and change its frame to whichever view you want to hide. Then bring this standalone view to front of the view you want to hide, you could add it using insersubview :abovesubview and variants of it. For more on it, see this.
This obviously requires that you are able to access them all using specific outlets, or through tags that you know of from subviews array, to allow conditional masking.
If you would simply want to hide it instead of "masking" with some other content, your obvious choices are:
set it's hidden property to YES.
set it's alpha property to 0.0 (or anything for fade out effect)
masking means autoresizing mask if it is yes means follow this
UIView *customView = [[UIView alloc] initWithFrame:frame];
[customView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight];
follow this link may be useful to you UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C?
Related
I am having trouble understanding how layering is performed within Xcode. I have a .xib with a view, that view has 6 UIImageView 's which each contain a different image. I have created IBOutlets for each of these so I can change the image at runtime.
I now want to change the order the UIImageViews are drawn at runtime, so imageA at the back is now drawn on top of everything else. How can I change the layer order? I originally tried removing all the subviews and using [self.view insertSubview: atIndex: ] to see if I could change the draw order that way but my code crashes.
Can anyone tell me the correct method for doing this please?
Thanks in advanced,
Elliott
i guess you are looking for these UIView methods:
1:
exchangeSubviewAtIndex:withSubviewAtIndex:
Exchanges the subviews at the specified indices.
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2
2:
bringSubviewToFront:
Moves the specified subview so that it appears on top of its siblings.
- (void)bringSubviewToFront:(UIView *)view
3:
sendSubviewToBack:
Moves the specified subview so that it appears behind its siblings.
- (void)sendSubviewToBack:(UIView *)view
with these methods you can exchange the subviews order to get one UIView appear over one other in their superview... if that's what you were asking....
A crash may be because when you remove the image from its parent, its retain count goes to zero and is deallocated.
If you just want to swap two views, see - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2 on UIView. If you want to do a more complicated exchange, you can remove and readd, just make sure you retain the view you remove (ARC notwithstanding).
Basically I want to have a custom UIView that has a bunch of buttons in it. Should I just add the buttons to the view inside initWithFrame? I also would like to set up all their locations in the view at the same time. Is there a customary place where all this would be done? I'm guessing I shouldn't do anything in drawRect since I'm not making any custom drawings, just adding buttons as subviews. Thanks.
initWithFrame: is a good place to add the buttons.
If the buttons' frame depends on the parent view's size, you should set the frame of the buttons in the parent view's layoutSubviews method. Otherwise you can just specify the buttons' autoresizingMask when you add the buttons.
You can't set up all their locations "at the same time", but if you call many addSubview: sequentially you don't see even that were added one at a time.
You do not always need to subclass UIView(if it is not particularly complex), you can create it at run time, add the components that you want with initWithFrame: and show it; otherwise you can subclass UIView just to create the layout (rounded border, gradient color, etc), initialize it from your class and add the components. If you have some problem, show us more details about your problem.
I have a UIView which contains a zoomable UIImageView and also another semitransparent UIView on top of that.
What I am trying to achieve is to be able to zoom the UIImageView while keeping the semitransparent view static and not zoomed.
If I add the semitransparent UIView on top of the UIImageView (which is added to the UIScrollView), everything zooms. However, if I add both as subviews to the base UIView, the touches only get tracked is the semitransparent UIView since its the last one added.
I do need control to reside first at the semitransparent UIView for the touches since I may want to resize the semitransparent view. However, I'd like to pass control of the touches to the UIScrollView if two fingers are used. Is there anyway for me to achieve this? The nextresponder doesn't seem to work. I also tried to use hittest in addition to subclassing UIWindow, but the base UIView needs to push/pop navigation controlling ability so I don't think I can subclass UIWindow to push onto the navigation stack.
Any help would be appreciated.
Thanks,
Winston
Hm.. you can try this hierarchy (possibly subclasses):
UIView (container)
> UIView (semitransparent overlay)
> UIScrollview
- UIView (zoomable content)
Like this, the overlay does not scale.
The tricky thing then is the user interaction on multiple layers. Its easy if there are areas in your overlay that should not detect user touches, for that you just set the UIView property 'userInteractionEnabled' to 'NO' for the view parts where touches should be 'forwarded' to the underlaying layers.
But if I get you right, you need something more complicated. You probably could set up some kind of master-touch-controller in the container UIView, that finds out what is happening and then calls certain methods of its subviews / forwards the events.
I don't know all the exact methods you need to override/implement in the container, but check out the tapZoom demo from the ScrollView Suite sample code. It's a pretty nice example there.
Just out of curiosity, may I ask what this interaction model is used for?
I am trying to get a UIView to clip to subviews but not entirely. I mean like an alpha change, outside the bounds
Edit:
I want this to be like the keynote mask feature for an image in the iPad.
You aren't going to be able to change alpha outside the bounds of a view, as you indicate. But a better solution would be:
Create a new view that extends the entire area. Set the alpha of this view to the desired value. Add another view inside this view, set clipsToBounds = YES on this view.
You can achieve this by using 2 views, but I don't think you can with 1.
so I have a UIImageView in a UIView but when I run it in the iPhone Simulator, the image goes beyond the boundaries of the UIView. What's wrong...?
Have a look at the clipsToBounds property of UIView. If you set this to YES on your view the UIImageView shouldn't extend outside of that view any more.
You may also benefit from using the Content Mode property in which you can specify how the content is drawn to fit the view. You can set it to fill, fit-to-scale, etc.