简体   繁体   English

当模拟器设置为横向模式iOS时,查看不滚动

[英]View not scrolling when simulator set to landscape mode iOS

I went through many blogs, stack flow questions and googled alot, but didn't get any solution to get my ScrollView work. 我经历了很多博客,堆栈流问题和googled很多,但没有得到任何解决方案来让我的ScrollView工作。 I don't know why but my ScrollView works in portrait mode but not in landscape. 我不知道为什么,但我的ScrollView在纵向模式下工作,但在横向模式下不工作。

I am developing using xcode 5 for ios 7 devices. 我正在使用xcode 5开发ios 7设备。 I developed my screen using storyboard. 我用故事板开发了我的屏幕。 I added a ViewController on storyboard, removed the default UIView it contains, and added a ScrollView. 我在故事板上添加了一个ViewController,删除了它包含的默认UIView,并添加了一个ScrollView。 In that ScrollView I added my subviews. 在那个ScrollView中我添加了我的子视图。 Then I referenced the ScrollView to the scrollview object in the outlets. 然后我将ScrollView引用到出口中的scrollview对象。

This is my LoginViewController.h 这是我的LoginViewController.h

#import <UIKit/UIKit.h>
@interface LoginViewController : UIViewController
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@end

This is my LoginViewController.m 这是我的LoginViewController.m

#import "LoginViewController.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
@synthesize scrollView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

My scrollview scrolls when in portrait mode but not in landscape. 我的滚动视图在纵向模式下滚动但在横向模式下滚动。 Please help me. 请帮我。 Thanks in advance. 提前致谢。

Reset scroll view content size on screen orientation: 在屏幕方向上重置滚动视图内容大小:

scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);

Scroll view frame most probably will change so you need to re-set content size. 滚动视图框架很可能会更改,因此您需要重新设置内容大小。

if you have auto layout on set content size inside layoutSubviews: 如果你在layoutSubviews中设置内容大小的自动布局:

- (void) layoutSubviews
{
    [super layoutSubviews];
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
}

Note 注意

Content size supposed to be larger than scroll view size. 内容大小应该大于滚动视图大小。 to scroll right? 向右滚动?

You want to add scrolling in scroll view check scrollView height. 您想在滚动视图中添加滚动检查scrollView高度。

If scrollView.height > scrollview.content.height scrollview did not scroll. 如果scrollView.height > scrollview.content.height scrollview没有滚动。 But scrollView.height < scrollview.content.height scrollview will scroll.... scrollView.height < scrollview.content.height scrollview将滚动....

So, the scrollview's content height is greater than the scrollview height is necessary for scrolling.. 因此,scrollview的内容高度大于滚动视图所需的滚动视图高度。

Therefore, you should do something like this: 因此,你应该这样做:

scrollView.contentSize.height=scrollView.height+50

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM