iOS subviews of window not rotating - ios

I have a number of subviews on the main window. These subviews do not rotate. Is there an easy way to fix this problem?

Use UIViewController, it handles rotation automatically. There's rarely the need for adding subviews directly to the window.

Related

Disable rotation animation of one subview

I'm trying to disable device orientation animation for just some subviews in my UIView subclass. I managed to disable camera preview rotation animation by using this view layer as AVCaptureVideoPreviewLayer and reacting to UIApplicationWillChangeStatusBarOrientation by changing layer.connection.videoOrientation. This works ok, but I also have one subview added that I also want to rotate without animation. Is there some way I can remove this animation for just that subview? Also I can't use willAnimateRotationToInterfaceOrientation as it's in UIViewController, and I want my UIView subclass to work this way without implementing anything additional in the controller. I only have UIView functions and NotificationCenter to work with.

Touch input not being recognized, totally stumped

I have a custom view (1) in the storyboard, and in that view's initialization it creates another view (2) and adds it as a subview. View 2 has implemented touchesBegan, touchesMoved, etc. The view controller has implemented touchesEnded. When I run it and do touches, only the view controller's touchesEnded is called. I don't know why view 2 isn't picking up the touches.
It appears view 2 is loaded properly because the custom background shows up.
I have tried explicitly setting userInteractionEnabled.
Now, get this. If I restart the iOS simulator, and run it, it works fine! But not if I run it a second time. And it never works when I run it on an actual ipad.
I don't expect anyone to magically know what the problem is, but any tips on how to debug this would be much appreciated!
Edit: initialization code for view 1...
self.myView2 = [[View2 alloc] initWithFrame:self.bounds];
[self addSubview:self.myView2];
For touch to happen, there are 4 conditions :
View should be userInterctionenabled.
Parent view should be userInteractionenabled
View should be in frames of parent view
View should not be hidden.
Check if all things are fine or not
I would also check to make sure that the frame of the parent view is large enough to hold the subview. The subview might be displayed but its touch area is being constrained by the frame of the parent view.
Of course, this situation would only happen if you set setClipsToBounds to FALSE in the parent view.
View 2 might be bigger than view (1)
It turns out that the "Autoresize Subviews" checkmark was the culprit. I designed my storyboard in portrait and was testing it in landscape. It seems that in landscape mode, the parent view was disappearing due to the rotation, but somehow the child view remained, giving the appearance that all was well.
When I unchecked "Autoresize Subviews" in the parent view, it worked.
Why rotation would make the parent view disappear is beyond me (I tested without a child view and that's what happened). And why the child view would be able to remain without the parent is also beyond me. If anyone can shine some light on this I would be happy to learn something. But right now I'm just happy that I didn't have to shoot myself.

UIScrollView unwanted scrolling after addSubview or changing frame

I have a UIScrollView filled with subviews, all is well when creating it and initially filling it.
But when I add a new subview that is positionned outside of the visible screen portion, or when I just resize an existing subview that is also outside of the visible screen portion, there is a subsequent 0.3s-long scroll animation (I can see it happening from my delegate) that seems to match the newly added/resized element.
Attempts:
pagingEnabled is always NO.
Setting scrollEnabled to NO during subview manipulations doesn't help.
Doing a setContentOffset:animated:NO after subview manipulations doesn't prevent the animation.
One single giant subview with all my subviews in it doesn't help.
My current workaround is to initially set the frame to fit inside the visible screen portion, or doing resizing work inside another superview, but it feels dirty, and won't handle all situations...
Is there a way to prevent this automatic scrolling animation when programmatically manipulating subviews?
Xcode 4.3, iOS SDK for 5.1.
I too discovered this problem and found this solution http://www.iphonedevsdk.com/forum/iphone-sdk-development/94288-disabling-uiscrollview-autoscroll.html
It involves subclassing the UIScrollView and entering no code in the following method.
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
}
Like the guy says on the link I've found it works and no problems so far. Hope it works for you.
I had this problem because I set the content size of the scroll view prior to adding the subview.
As soon as I change the code so that the content size of the scroll view was set after adding the subview the problem went away.

Can a UIView "ignore" interface orientation?

I have a UIView associated with it's own UIViewController that I want to always be locked to portrait mode.
Imagine that it wants to act like a window into the internals of the iPhone. Weird, I know.
That view will have subviews on top of it, or perhaps other viewcontrollers' views which want to respond appropriately to interface rotation. I basically want to lock a single view to portrait while letting my implementation of shouldAutoRotateToInterfaceOrientation handle the rest.
Is there a way to do this?
I believe adding it as a subview of the main window will prevent it from being autorotated (because only UIViewControllers handle rotation). Calling [self.view.window addSubview:internalView]; should work perfectly. (though there are no guarantees as to what orientation the view is in when it is added).

Main view that should not rotate but subviews that should, including UIPopoverController

I would appreciate some help before spending any more time on trial and error.
Imagine the following: I'm just starting to create something for the iPad that will look something like a dashboard with a number on dials on it. When rotating the iPad (portrait, landscapeLeft etc) the background should not rotate, the dials position should remain but the inside of the dials should rotate to correct position. So, main view should not rotate, but the subviews (inside of the dials) should. I have done this on the iPhone before by telling the viewController to only be in portrait and then checking UIDeviceOrientation, so I thought this was gonna be easy. But my headache starts when displaying a UIPopoverController. Since I'm not changing the UIInterfaceOrientation, the UIPopoverController will always be in portrait.
Ideal solution would be to have the main view (self.view from the viewController) not observe changes in rotation, but allowing the subviews to do it, but from what I understand that is not possible. Only other solution I could think of is to not animate the change in rotation, and jut move the subviews (dials) into their new position. Animating them (subviews) make the dance all over the place. But I have not found any good solution on how to do that.
Any thoughts anyone?
You are correct in thinking that if the main view does not rotate, having the subviews automatically rotate is not possible.
A workaround that springs to mind is this: What is animated when you rotate the view is a change in the view's transform. I am pretty sure that you could register to receive device orientation changes and when you get the change, animate a transform change to a container view that contains all the subviews you want to rotate.
Edit: just read about you having a popover controller.
As far as the popover is concerned, the way the API manages autorotation is to hide the popover and then show it again at the end of the rotation. It shouldn't be too hard to implement similar behavior.
Another thing that occurs to me is this: Is what you want to not rotate just a background? Would it work to just have two backgrounds, one for portrait and one for landscape that you could switch between? It might not be the most pretty looking, but it would probably be easier than recreating autorotation behavior yourself.

Resources