Overriding internal UIViewController methods because an internal method is giving me bugs - iphone

What are the consequences of overriding internal UIViewController methods?
[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:] is giving me some problems. It resizes my frame to values I do not desire sometimes. I do not even know where it picks off the new frame's values (it is close to the size of the superview to where I am adding it, but is off by 2px). Reference: this question, which I also need some help with.
I tried defining an empty - (void)viewDidMoveToWindow:(UIWindow *)window shouldAppearOrDisappear:(BOOL)flag method in my view controller. Bug is gone. >.<
Does anyone know if overriding -viewDidMoveToWindow:shouldAppearOrDisappear: is ok to do? Or some other workaround?
Or, does anyone know when and why -viewDidMoveToWindow:shouldAppearOrDisappear: likes to change my frame dimensions sometimes?
Thanks in advance.

If you override that method you run the risk of getting rejected, it is undocumented. An approach that isn't against the rules is to override the setFrame: method of the view in the view controller though.
If you believe this is a bug, put together a minimal test case and submit it to http://bugreport.apple.com and potentially upload it somewhere that we can see exactly what is happening as well.

For the "why" part of your question; the frame can automatically change in two situations:
Your ViewController is part of a view controller hierarchy (Navigation or TabBar). In this case your parent ViewControllers do as they please with your views frame.
Your view allows autosizing and the parent view changes size. The thing to note here is that the size of the root view is changed to fit the screen, causing autosizing to happen for all subviews.
To find the exact culprit in your case you could set a breakpoint in the overridden setFrame:.

Related

Trying to Create a mostly custom TableView

I'm trying to make a custom table view. I have a UIScrollView loaded up with UIViews. I would like to recreate the reusable cell functionality that a tableview has. And I'm wondering what might be a good way to do this.
I was thinking that when the UIViews are scrolled off screen I'll remove them from the Subview, and when I need another I'll just add it. However I'm not sure what method might be the best to do this in.
I could check to see the views location in scrollViewDidScroll: but I'm not sure if this would be the best thing to check over and over again. If anyone has any suggestions or helpful tips that would be really awesome.
Thanks!
One reason why scrollViewDidScroll is less than ideal choice is that your scroll view subclass will then need to be its own delegate. That will deny the users of that class the chance to be the delegate (easily, at least).
The better place to check this is in overriding layoutSubviews. And yes, like scrollViewDidScroll, it gets called a lot! But if you think about it, the only way to detect with certainty if any subview has been scrolled off, is to check when any scrolling occurs.
So the key is to be as efficient as you can checking. The first thing I'd try is fast enumeration of the subviews, asking if each frame transformed by contentOffset falls within the parent's bounds (using CGRectIntersectsRect). If not, add it to your reuse pool and remove it from superview.
Subclass UIScrollView and override setContentOffset. That's where the scrollViewDidScroll message comes from in the original UITableView, and that's where you can implement your own logic.
Edit: you might want to study the source code for PSTCollectionView, which is not a custom UITableView but a custom UICollectionView.

How do i make orientation work in my app?

The way my app currently deals with orientation is it repositions all of the items in the view, the layout of which can change as the user interacts with it. I've had numerous problems, such as view's not appearing, views changing before the screen rotates etc. I'm wondering the best way to deal with orientation?
If the landscape-layout is completely different from the portrait-layout I just load all subviews in the init-method of my UIView-subclass and added them as subviews.
The whole magic is done in the layoutSubviews-method where I only check in which orientation I am at that moment. Never call alloc, addSubview, removeFromSuperview, ... methods in layoutSubviews. The layoutSubviews should only contain code that sets the frame-properties of subviews.
Referring to your problems:
view not appearing: maybe forgot an addSubview-call
views changing before the screen rotates: you probably update some frame-properties of subviews outside the layoutSubviews-method
One possibility - if your app only works with one orientation, disallow orientation changes. This is a reasonable response, some apps are only usable in one view.

Problem autosizing second view

Once a view is called from appdelegate, it is properly loaded but not autosized correctly, on the bottom you see last lines from previous view! On xib file, view mode property is set to scale to fill but I tried others and still happening the same... Thanks for any idea to solve it!
Maybe check Interface Builder, is the view using any of the Simulated User Interface Elements? That isn't directly answering your question but it might be worth a look, e.g. the status bar is normally simulated by default.
The standart way to control autosizing is by
self.View.autoresizingMask = UIViewAutoresizingFlexibleHeight(or st. else)
self.View.autoresizesSubviews = YES;
You might also try to just set the size manually

Iphone View explanation in detail (View, Superview, etc)

I'm looking for an in-depth breakdown/explanation of the iphone's view usage. Like, what controllers have what types of views, how they relate (child <> parent), how they can be nested, added and removed, etc.
Preferably something with some pictures would be nice too (I'm a visual learner).
But yeah, in-depth, technical, explanations of the iphone view system when used in IB/Obj-c would be awesome.
Also, feel free to give your breakdown or post to resources and I'll do the research.
Thanks
EDIT:
Ok, I'll be more specific. Is the View a stack - is it a queue? What does it look like when I call addtosubview?
What happens if the view isn't a full UIView, but say a smaller UI Control - will it be visible?
Say I have a UIView with a UITabView (2 items) and one content view is a UITtableView.
What's the parent view? What is the order of the children? Is that all dependent on how I add them to the view? In this case, the UITabBar control takes care of handling the views when I select the button.
When I call insertSubview how do I know what index to position it at?
This will be a good place to start (go from here to UIViewController and others that seem relevant). A UIView can contain many subviews of any type (they all inherit from UIView). To nest views, you add the subview to the superview [superview addSubview:subview]. You should also read up on Model-View-Controller.
Edit:
This SO question might also shed some light on the matter.
Edit edit:
Best I can do to answer your questions:
UIViews have an iVar subviews which is an array of subviews. Each of these also has such an array and can contain UIViews.
I assume you mean not full screen, generally, the topmost view is a UIWindow, and to this you add any UIView subclass you like, such as UISlider.
Your UIView has a subview UITabView, I don't know specifically, but I would guess it has two subviews, one of which is visible at a time, and one of these is the UITableView. Order is dependent on the order you add them in, and they will overlap each other depending on this order, but it can be changed with sendSubviewToBack and bringSubviewToFront.
If you want a specific view order, you're probably better off using insertSubview:aboveSubview: and the equivalent below, rather than at index.
Hope some of this helps.
Ok, I'll be more specific. Is the View a stack - is it a queue? What does it look like when I call addtosubview?
I like to think of it as a tree. The window is the root node, and it has any number of subviews. Those subviews can have any number of their own subviews, going down as far as necessary to create the full interface. The addSubview and removeFromSubview methods manipulate a view's "children".
What happens if the view isn't a full UIView, but say a smaller UI Control - will it be visible?
My understanding is that everything on the screen is a subclass of UIView, even the UIControl objects. Therefore, they behave mostly the same.
Say I have a UIView with a UITabView (2 items) and one content view is a UITtableView. What's the parent view? What is the order of the children? Is that all dependent on how I add them to the view? In this case, the UITabBar control takes care of handling the views when I select the button.
I'm not sure: David's answer has more info that should help.
When I call insertSubview how do I know what index to position it at?
I wouldn't worry about it: most of the time you just want addSubview and you won't care about the internal order.

iPhone custom UIView bounds

I am trying to create a custom UIView that is stored in a nib so that I can position it using Interface Builder.
I have created a class called RippleView that extends UIView. I'd like to position this view as a subview using Interface Builder. To do so, I've dragged a new view into my existing view in Interface Builder and gave it the RippleView class identity. I then linked my RippleView outlet to the view I just created.
In the RippleView class, I've implemented initWithCoder which doesn't do anything other than call [super initWithCoder...]. In awakeFromNib I go ahead and initialize my code.
Now, for some reason, if I try to check the bounds of the RippleView in awakeFromNib, I get ridiculous values (0 width and 1081171968 height). These bounds don't change in drawRect, so I don't think it's an issue of the view not being initialized. I get similar values in my touchesMoved event handler.
I had no problems when I was programmatically creating the subview (initWithFrame). What could be causing the frame bounds to go haywire?
Solved! The issue had nothing to do with the code. The UIView bounds are floats and I was printing them as integers. It's things like this that separate the programmers from the dropouts :p
Can you post your initWithCoder: implementation? If you're not setting the value of self, or if you've misspelled initWithCoder:, you might see these sorts of problems. Have you tried setting a breakpoint in initWithCoder: to make sure it gets called?