Say I have a custom UIImageView with several subviews.
In a UIViewController, if I create the custom imageView and add it as a subview to self.view, does that mean all the custom view's subviews will display on the screen, or just the custom view without its subviews?
The whole view hierarchy: the current view, it's subviews, their subviews, and so on, will be added to self.view's view hierarchy.
Related
I have a UIScrollView that contains a few custom UIViews that I can drag around by implementing the touchesMoved method. However, I need to be able to drag this custom view out of the scroll view and have it added to a custom UIView that is to function as a sort of dock for the view that is being dragged.
How would I go about implementing this? As soon as I remove the custom draggable view from its superview, it disappears (understandably). Thank you.
You have to have the view as a subview of the same view at the beginning and end of the drag.
In order to have it as a subview of another subview of this main view, you would have to destroy and recreate it at the appropriate place. You could do this both at the beginning and at the end of the drag process.
I'm writing an UIPageViewController based application over which view I placed a subview:
[self.view addSubview:fontViewController.view];
This view fade in and out when I tap on the center of the main view (the pageviewcontroller one). When this view is shown, I don't want that disappears when I change the page. I think that this subview should be added to a layer over the main view and not to the view itself. Am I wrong? If not, how can I do that?
What is the parent of the main view?
In some cases this might work:
[self.view.superview addSubview:fontViewController.view];
I have a uiview that has all my buttons, that has its controller. How can I include the button view in all my uiviews using the interface builder? The buttons must work when click. The purpose is to increase maintainability.
You can create a custom UIView in interface builder like this:
Loading custom UIView from nib, all subviews contained in nib are nil?
Then add your custom view to other views in IB by adding a normal UIView and changing the class to your subclass.
Try this. I'm not sure that adding UIView in Interface Builder will work, but adding subview programmatically always works.
From my understanding, setNeedsDisplay only affects the view it's called on. Is there a simple way to say "update this view and all its subviews, recursively?"
In response to the comments, here's my situation: I've got a custom view
#interface ContainerView : UIView
this view does not implement drawRect. In my xib there's an instance (called container) of the ContainerView which has some (custom) subviews added to it. When in the code I say
[container setNeedsDisplay]
I expect these subviews to update. Where am I wrong?
Ok, UIView draws itself when its first displayed. CALayers do not. Calling setNeedsDisplay on a UIView marks it as dirty, this automatically redraws all SubViews as well (calling drawrect on all subviews). Calling setNeedsDisplay on a CALayer doesn't have the same effect, it wont redraw sublayers. Hope this helps.
Regards
Ref
iOS 7 Programming Pushing the Limits By Rob Napier, Mugunth Kumar
UIView Class Reference
The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content
setNeedsDisplay
Marks the receiver’s entire bounds rectangle as needing to be redrawn.
Note: If your view is backed by a CAEAGLLayer object, this method has no effect. It is intended for use only with views that use native drawing technologies (such as UIKit and Core Graphics) to render their content.
The subviews are inside the bounds of the view, so the view will ask it's subview what to display.
Have you try and encounter some case that go agains this definition?
If you are implementing your own UIView subclass you need to handle all the display yourself in drawRect:
In a UIScrollView, I have a subview. How do I determine the frame of the subview in terms of the UIScrollView's container view coordinate system?
You can simply use the following.
[scrollViewSuperview convertRect:subview.frame fromView:subview]
You can find more info about this on http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/uiview_class/uiview/uiview.html.
Attribute view.frame is always related to parent view, so frame of a subview will be a rect inside the scroll view where your subview is placed.