Unable to add UIScrollView to UITableView - objective-c

UPDATED -- I have an iPad app that was originally designed and written for portrait mode only; I now want to add a UIScrollView so it will scroll in landscape mode. Auto Layout is checked and the different scenes are built using Storyboard. (I am following this tutorial). The major problem is when switching from portrait to landscape, the bounds of the frame change drastically, thereby causing problems with the logic of the scroll.
This is the image of the first scene (UIView) I am trying to add a UIScrollView to:
This is what it looks like in landscape mode (w/o scrolling):
This is my code in the -viewDidLoad method for that scene:
// create UIScrollView
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,self.bookDetailView.frame.size.width, self.bookDetailView.frame.size.height)];
scroll.delegate = self;
scroll.pagingEnabled = YES;
scroll.scrollEnabled = YES;
scroll.showsVerticalScrollIndicator = YES;
CGSize scrollableSize = CGSizeMake(768, 1024); // size of portrait mode
scroll.contentSize = scrollableSize;
[self.view addSubview: scroll];
The scroll bar (as thin as it is) now shows, BUT although it moves like it should, the UIView doesn't move. Portrait mode works fine (no scrolling needed) but landscape mode doesn't scroll at all (even tho' the vertical scroll bar does move). I'm wondrering if I should abandon the idea of using scrolling for landscape mode and create separate scenes for landscape mode instead.

Is there any reason why you need a separate UIScrollView ? UITableView is already a scroll view. My guess is that the touch events from the UIScrollView you created are interfering with those of the UITableView.

Related

iOS7 UIWebView scroll indicator disappearing

I've noticed weird behaviour in my UIWebView using iOS 7.0 SDK. This behaviour wasn't there in iOS 6, and my code regarding this webview hasn't changed.
I have a storyboard, not using autolayout, autoresize mask for the webview is everything selected.
I have scale pages to fit set to YES.
When I load a local html file, it displays fine and upon scrolling, the webview will show the scroll indicator.
When I rotate the device, the webview resizes fine, it's rect is correct size, the page displays as expected, however the scroll indicator is not visible
Here's the kicker. If I don't scroll before I rotate, the scroll indicator is visible when I start scrolling AFTER the rotate.
If I DO scroll before the rotate, the scroll indicator is not visible after the rotate.
I checked the html, there is no jquery or js that will change the overflow in regards to orientation.
OK I found the solution, I was using a subclassed UIWebView and overriding the layoutSubviews to not draw the gradient shadows above and below the scrollview (as it has by default).
- (void)layoutSubviews
{
[super layoutSubviews];
// hide the shadows at the top and bottom of the webview's frame
for (UIView* shadowView in [self.scrollView subviews])
{
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
}
}
}
Well guess what was a UIImageView? The Scroll Indicator! I removed this code and the problem went away

UIBarButtonItem with portrait and landscape images may show portrait image for landscape orientation

In iOS 7, I am showing a UIBarButtonItem in the navigation bar of a UINavigationController. The UIBarButtonItem has an image for portrait orientation and another image for landscape orientation:
// "self" refers to the UINavigationController
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:portraitImage landscapeImagePhone:landscapeImage style:UIBarButtonItemStylePlain target:nil action:NULL];
If you just rotate the interface, the correct image is used for the interface orientation. However, if the view for the UINavigationController first appears in landscape orientation (say, by presenting it in landscape orientation or by dismissing a view controller that is covering it up in landscape orientation), the portrait image is used instead! Why is this happening? Is this a framework bug? If so, is there a way to work around this, so the landscape image will always be shown for landscape orientation?
I'm having this same problem.
I found a solution over at UIBarButtonItem with separate portrait and landscape images - layoutSubviews not called when popping a view controller from UINavigationController that sounded really promising, but it doesn't work for me. I'm going to go with changing the image on orientation change instead. Please post your solution if you find something better.
The landscape image works all of the time on UIToolbar, but not UINavigationBar. With UINavigationBar, I get the problem you state. I also had the problem that if I tapped the button in landscape, it would change to its portrait version.

How to resize UIPageViewController in landscape

I am having an issue with resizing UIPageViewController in landscape mode.
Here is my scenario:
I have a UIViewController which has a scrollview in it.
I added a UIPageViewController to the scrollview programmatically and it is displaying all my view controllers in each page correctly.
When i change the device orientation to landscape i am correctly changing the content size of the scrollview but the PageViewController is displaying only up to half of its contents. Can you please help me with this one.
Thanks,
Anand.
It looks like you are either not getting the correct orientation of your device or trying to set it in the viewDidLoad function. The following code should correctly set your page view controller's frame to current orientation.
- (void)viewWillLayoutSubviews {
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
screenFrame = CGRectMake(0, 0, screenFrame.size.height, screenFrame.size.width);
}
//Please don't forget to replace _myPageViewController with your variable
_myPageViewController.frame = screenFrame;
}
Please note that code is getting called from viewWillLayoutSubviews
function.

UITableViewCells scroll horizontally using auto layout when changing to portrait mode

I am creating a screen that is primarily a UITableView with cells that have differing controls and layouts. I had the tableview working great until I switched to portrait mode where it allowed the tableview cells to scroll horizontally. The cells should never scroll horizontally; only vertically. When I navigate to the screen and I'm already in portrait mode it works fine, even going to landscape and back to portrait.
Some things to note are that I'm using and iPad, storyboards, auto layout, and the UITableViewCell is custom and does not have a nib (though it does the same thing when I tried using a nib). All controls and layout constraints are added programmatically. I am using a custom view controller with a view inside of it. Inside of the view I have the tableview and a toolbar. If I remove the view so that the tableview is directly under the custom view controller it works fine, but then I can't have the toolbar.
Here is the test code I am using to troubleshoot the problem:
// Remove all existing subviews
for(UIView *subView in self.contentView.subviews)
{
[subView removeFromSuperview];
}
UILabel *testLabel = [[UILabel alloc] init];
testLabel.text = #"Test";
[testLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
self.contentView addSubview:testLabel];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(testLabel);
NSString *format = [NSString stringWithFormat:#"V:|-[testLabel]-|"];
NSArray *constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:nil metrics:nil views:viewsDictionary];
//[self.contentView addConstraints:constraintsArray];
The scrolling works as intended until I uncomment the line to add the constraints. This code is in a method of my custom cell and gets called every time a cell is dequeued. I've tried adding the contraints individually, without using the visual format, with no luck. I have also done some research and found the code that removes the contraints from the cell and adds them to the contentView, but that hasn't helped either.
What do I need to do to keep the cells from scrolling horizontally when changing from landscape to portrait mode?
MORE INFO:
After some more debugging I found that the table view's content size is not shrinking horizontally. The width is staying at 1024.

Stop UIToolbar from Disappearing in Landscape Orientation - iPad

Not sure why this happens or how to stop it, my UIToolBar on the details viewcontroller is only visible during portrait view. I want it visible at all orientations. How do I do that? Thank you.
I encountered the same problem by just dragging a UIToolBar on to my view and docking it on the top of the window. It showed up in landscape but not portrait. Interface Builder - at least the one embedded in Xcode 4 - doesn't seem to do the right thing with the resize masks.
While Kshitiz's answer above will work, it has a couple of flaws. As coded, it does not support all four orientations. More importantly, it's not resolution independent.
A better solution is briefly described in enamrik's comment, so credit should go to him/her. Here are the steps:
Select the tool bar in Interface Builder.
Open the Size inspector.
In the Autosizing box, select the left, right and top "i-beams" on the exterior of the square. This keeps the position of the toolbar fixed relative to the sides of the view when the view is resized.
Inside the Autosizing square, select the horizontal line with arrows on both ends. This causes the size of the toolbar to change in sync with the parent view.
in your this function of view controller reset view frame bro
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
self.view.frame = CGRectMake(0, 0, 703,768);
} else {
self.view.frame = CGRectMake(0, 0, 768, 1024);
}
return YES;
}
and your tool bar frame too
good luck
Faced the same problem when I add UIPickerView programmatically and add UIToolBar for the PickerView. Just need to add [.flexibleWidth,.flexibleHeight] for the UIPickerView. eg:-
let statTypePicker = UIPickerView()
And then add
self.statTypePicker.autoresizingMask = [.flexibleWidth,.flexibleHeight]