Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correctly Rotating a UITableView with Transform without messing around with Interface Builder

UiTableView does not provide Horizontal Scrolling in landscape mode.

My UiTableView is not full screen (So I cannot just use it horizontal mode while rotating my content, which are pictures)

I found that to rotate my UiTableView using Transforms.. I have to position it in Interface Builder in some weird position so that when rotated, it just looks right.....

Has anyone done this without messing around with Interface Builder ? Same code?

like image 792
user82383 Avatar asked Jan 23 '23 14:01

user82383


2 Answers

Ok, the problem was that since the width of the tableview was flexible, after the rotation the width of the cells (which is actual the height) were increased too. I overcame the problem by subclassing the UITableView and overriding the layoutSubviews method, so it looks like this now:

- (void)layoutSubviews {

    [super layoutSubviews];

    for (UIView* child in [self subviews]) {

        CGRect frame1 = child.frame;
        if ([child isKindOfClass:[UITableViewCell class]]) {
            frame1.size.width = 120;
            child.frame = frame1;        
        } 
    }

}
like image 116
Umair Aamir Avatar answered Jan 31 '23 08:01

Umair Aamir


See the Autoresizing Behaviors section on the Windows and Views conceptual guide on Apple's site. That explains how to achieve automatic rotation from portrait to landscape and back again without using Interface Builder and without having to deal with manually implementing complex transforms.

As for rotating your custom content and disabling scrolling, see shouldAutorotateToInterfaceOrientation:interfaceOrientation, willRotateToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: in UIViewController. You can implement these methods to intercept rotation events, check which orientation you are moving to, and take care of your custom drawing and properties setting stuff there.

Edit

In response to your comment, I found this bit from the Table View Programming Guide from Apple:

UITableView inherits from UIScrollView, which defines scrolling behavior for views with content larger than the size of the window. UITableView redefines the scrolling behavior to allow vertical scrolling only.

Perhaps Apple has deemed horizontal scrolling of table views to be against their human interface guidelines. I wonder if you could re-re-define the scrolling behavior if you subclassed UITableView, but that would probably be a ton of trial and error work. If you are actually able to accomplish this, I'm sure many people here would be interested in how you did it. I searched around for a while trying to find other people's solutions, but I couldn't find anything out there.

Then again, Apple may not allow your app into the app store if you did this, assuming it really does break the HIG.

like image 37
Marc W Avatar answered Jan 31 '23 09:01

Marc W