简体   繁体   English

为什么我的UIScrollView不起作用?

[英]Why does my UIScrollView not work?

I have placed a UIScrollView in a ViewController in IB and gave the view the tag:1 . 我在IB中的ViewController中放置了一个UIScrollView ,并为视图提供了tag:1 In viewDidLoad: , I have this code: viewDidLoad: ,我有这个代码:

UIScrollView *scrollView = (id)[self.view viewWithTag:1];

scrollView.backgroundColor = [UIColor clearColor];
scrollView.opaque = NO;

[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 800)];

I have a slider and a label in the ScrollView just to see if it scrolls, but it doesn't scroll at all. 我在ScrollView有一个滑块和一个标签,只是为了看它是否滚动,但它根本不滚动。 I get to change the backgroundColor to for example yellowColor , but it doesn't scroll. 我将backgroundColor更改为例如yellowColor ,但它不会滚动。 Is there a method or action i have to add? 是否有必须添加的方法或操作? Please help! 请帮忙! :) :)

I had the same problem not long ago, but this did the trick. 我不久前遇到了同样的问题,但是这样做了。

Use -(void)viewDidAppear:(BOOL)animated { ... } 使用 - (void)viewDidAppear:(BOOL)动画{...}

-(void)viewDidAppear:(BOOL)animated  {

    [super viewDidAppear:YES];

    scrollView.backgroundColor = [UIColor clearColor];

    scrollView.opaque = NO;

    [scrollView setScrollEnabled:YES];

    [scrollView setContentSize:CGSizeMake(320, 800)];

    [super viewDidLoad];
}

Also declare the UIScrollView in your header 还要在标题中声明UIScrollView

@property (retain, nonatomic) IBOutlet UIScrollView *scrollView;

Try Like this 试试这样

UIScrollView *scrollView = (id)[self.view viewWithTag:1];
scrollView.frame=CGRectMake(0, 0, 320, 460);
[self.view addSubview:scrollView];
scrollView.backgroundColor = [UIColor clearColor];
scrollView.opaque = NO;
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 800)]; 

because when the scroll view height cross the current view height then only its get scrollable. 因为当滚动视图高度越过当前视图高度时,只有它可以滚动。

scrollView.backgroundColor = [UIColor clearColor];

change this to something bright like red or green for testing. 将其更改为明亮的红色或绿色以进行测试。

UIScrollView *scrollView = (id)[self.view viewWithTag:1];
scrollView.backgroundColor = [UIColor clearColor];

also make sure your scrollview isnt nil (after obtaining from [self.view viewWithtag:1] . otherwise the code seems OK 还要确保你的scrollview不是nil(从[self.view viewWithtag:1]获取之后。否则代码似乎没问题

EDIT 编辑

select scroll view in interface builder > go to attributes editor > tick scrolling enabled 在界面构建器中选择滚动视图>转到属性编辑器>启用勾选滚动

启用滚动

here may be its problem with set TopBar of UIView with UINavigationBar from xib and also BottomBar of that... Here from XIB first select main UIView after click on Attribute Inspector after in Simulated metrics set NavigationBar as a TopBar and after set frame of UIScrollView in XIB . 这里可能是UIView TopBar和来自xib的UINavigationBar以及它的BottomBar的问题......这里来自XIB首先在模拟指标中点击Attribute Inspector之后选择主UIView ,将NavigationBar设置为TopBar并在UIScrollView设置框架之后XIB

try with bellow code it will worked .. 尝试用波纹管代码它会工作..

[scrollView setContentSize:CGSizeMake(320, 844)];

try to add this code in viewWillAppear 尝试在viewWillAppear添加此代码

UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:1];
scrollView.frame=CGRectMake(0, 0, 320, 400);
scrollView.backgroundColor = [UIColor clearColor];
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 800)]; 
[scrollView setUserInteractionEnabled:YES];

hope will help you 希望对你有所帮助

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

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