关于Scrollview怎么只让一个方向滚动的问题

本文详细介绍了如何在ViewController与NavigationController的不同组合下,正确设置ScrollView的contentSize属性,以避免出现无法垂直或水平滚动的问题。通过测试发现,当ViewController包含导航栏时,ScrollView的contentSize的y值设置为SC_Height会导致垂直和水平滚动都能正常工作,而将其设置为0则仅保留水平滚动,并使view高度减小。这与ViewController的automaticallyAdjustsScrollViewInsets属性有关,该属性默认为Yes,用于自动调整ScrollView内边距以适应屏幕区域的变化。设置为NO后,开发者需要自行管理ScrollView的内边距。最终提供了解决方案:在代码中将ViewController的automaticallyAdjustsScrollViewInsets设置为NO,从而解决在有各种导航组件时ScrollView的滚动问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 这两天发现给Scrollview设置contentSize时总是会出现一些问题,楼主苦心专研终于搞懂了Scrollview的contentSize属性该怎么设置了大笑大笑大笑大笑

相信大家在用Scrollview时可能会遇到不知道怎么设置contentSize来达到自己想要的只垂直或者只水平滚动,而有的时候通过设置contentSize.y/x可以,有的时候又不行.这里有一些心得,希望能对你有所帮助奋斗

通过一些测试,发现Scrollview的这种情况和ViewController与NavigationController有关.

测试一:这是一段没有导航的代码

    [self.view setBackgroundColor:[UIColor grayColor]];
    
    _scrollview = [[UIScrollView alloc] init];
    // scrollview设置一个背景色
    _scrollview.backgroundColor = [UIColor greenColor];
    [_scrollview setBounds:CGRectMake(0, 0, SC_Width, SC_Height)];
    _scrollview.pagingEnabled = YES;
    [_scrollview setCenter:self.view.center];
    
    // 设置contentSize
    _scrollview.contentSize = CGSizeMake(SC_Width*5, SC_Height);
    [self.view addSubview:_scrollview];
    
    // 在scrollview上添加几个不同颜色的view
    for (int i=0; i<5; i++) {
        UIView *scview = [[UIView alloc] initWithFrame:CGRectMake(SC_Width*i, 0, SC_Width, SC_Height)];
        [scview setBackgroundColor:[UIColor colorWithRed:arc4random()%11/10.f green:arc4random()%11/10.f blue:arc4random()%11/10.f alpha:1]];
        [_scrollview addSubview:scview];
运行结果如下,正常


将contentSize的y设置为0

_scrollview.contentSize = CGSizeMake(SC_Width*5, 0);
得到的结果是和上面一样的,所以在没有nav时,ScrollView的contentSize这两种设置法都可以实现的

测试二:将ViewController加上导航栏

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    JLRootViewController *rvc = [[JLRootViewController alloc] init];
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:rvc];
    [self.window setRootViewController:nvc];
首先将 contentSize的y设置成SC_Height

_scrollview.contentSize = CGSizeMake(SC_Width*5, SC_Height);
测试结果如下图:


可以明显的发现,垂直和水平方向都可以滑动,可以明显的看到两个方向的滚动条

现在我们来将ScrollView的contentSize的y值设成0

_scrollview.contentSize = CGSizeMake(SC_Width*5, 0);

再次测试结果如下图

我们看到contentSize的y值设成0后,虽然垂直方向不能滚动了(没有垂直滚动条了,只有水平滚动条),但是view的高度貌似变小了,打开Debug View Hierarchy得到如下图



根据上图(绿色的为ScrollView,紫色的为加在ScrollView上的view)发现果然view的高度变小了,为什么会这样,原来是ViewController的一个属性automaticallyAdjustsScrollViewInsets搞得鬼,这是个Bool 的类型,默认是Yes的,这个属性有什么用,查阅苹果的官方文档有下面的一段话:

A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.

Declaration

SWIFT

var automaticallyAdjustsScrollViewInsetsBool

OBJECTIVE-C

@property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets

Discussion

The default value of this property is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

Availability

Available in iOS 7.0 and later.


这里面大概的意思是说这个属性是一个bool类型,它决定它自己(ViewController)中嵌入的ScrollView是否自适应,当这个值默认为yes时,它会使ScrollView自适应由于一些重要的bar的加入(navigation bar, and toolbar or tab bar)导致的屏幕区域不够用的情况(例如,当加入导航栏时,self.view会向下移64个点).设置为NO时你就可以自己去管理你的ScrollView以适应ViewController.


  好了,问题解决了,只要在代码中ViewController的automaticallyAdjustsScrollViewInsets 设置成NO就解决了这个问题了... ...

在原来的代码上加上代码

self.automaticallyAdjustsScrollViewInsets = NO;
运行如下:



运行完美大笑大笑大笑大笑大笑(再也不怕有各种bar了)

在Android或iOS等平台使用ScrollView做循环滚动播放的动画,通常需要结合Viewflipper、RecyclerView、CycleScrollView等组件。这里以CycleScrollView为例,这是一个专门设计用来做无限滚动效果的控件。 以下是基本步骤: 1. **引入库**:首先,你需要在你的项目中导入CycleScrollView库,对于Android,可以在Gradle文件中添加依赖,如果是iOS则可能需要在Swift或Objective-C中导入相应的框架。 2. **设置视图**:创建一个包含动画视图的布局,比如ImageViews或RecyclerView的item。动画应该能自动重复播放,比如使用`setAnimationDuration`设定每个动画循环的时间。 3. **配置CycleScrollView**:实例化CycleScrollView,并将其设置滚动方向(水平或垂直),同时启用循环滚动功能。你可以通过`isCycleEnabled = true`开启无限滚动。 ```java (for Android) CycleScrollView scrollView = findViewById(R.id.scroll_view); scrollView.setOrientation(LinearLayout.VERTICAL); // 水平或竖直 scrollView.setCycle(true); // 开启循环 scrollView.startAutoScroll(); // 自动滚动 ``` ```swift (for iOS) let cycleScrollView = CycleScrollView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)) cycleScrollView.orientation = .vertical // 水平或竖直 cycleScrollView.isCyclingEnabled = true cycleScrollView.startScrolling() // 开始滚动 ``` 4. **动画设置**:为每个显示的动画视图设置适当的动画,可以使用`startAnimating()`方法。如果动画持续时间已设,则在完成一次循环后会自动开始下一轮。 5. **监听滚动结束**:为了在动画结束后触发新的动画,你可以监听`onEndScrolling`回调,或者利用CycleScrollView的内置事件机制。 ```java scrollView.setOnEndScrollListener(object : OnEndScrollListener { override fun onEndScroll(direction: Int) { // 触发新的动画 } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值