让UIScrollView、UITableView的滚动条一直显示

本文介绍如何通过自定义UIImageView Category的方法来禁用UIScrollView和UITableView中滚动条的自动隐藏功能,确保滚动条始终可见。

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

先用xcode5.1.1或更低版本创建一个Category,如图:

然后拷贝以下代码到刚创建的UIImageView+ForScrollView.m文件中:

- (void) setAlpha:(float)alpha {
    
    if (self.superview.tag == noDisableVerticalScrollTag) {
        if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleLeftMargin) {
            if (self.frame.size.width < 10 && self.frame.size.height > self.frame.size.width) {
                UIScrollView *sc = (UIScrollView*)self.superview;
                if (sc.frame.size.height < sc.contentSize.height) {
                    return;
                }
            }
        }
    }
    
    if (self.superview.tag == noDisableHorizontalScrollTag) {
        if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleTopMargin) {
            if (self.frame.size.height < 10 && self.frame.size.height < self.frame.size.width) {
                UIScrollView *sc = (UIScrollView*)self.superview;
                if (sc.frame.size.width < sc.contentSize.width) {
                    return;
                }
            }
        }
    }
    
    [super setAlpha:alpha];
}

在.pch中定义这两个值:

#define noDisableVerticalScrollTag 836913
#define noDisableHorizontalScrollTag 836914

然后在你所用到UIScrollViewUITableView的视图控制器中:

#import "UIImageView+ForScrollView.h"

设置UIScrollViewUITableView:

    UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    scrollview.contentSize = CGSizeMake(320, 960);
    scrollview.tag = noDisableVerticalScrollTag;
    [scrollview flashScrollIndicators];
    [self.view addSubview:scrollview];
    
    //---
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 66, 320, 480)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = YES;
    _tableView.tag = noDisableVerticalScrollTag;
    [_tableView flashScrollIndicators];
    [self.view addSubview:_tableView];

如此即可让UIScrollView、UITableView的滚动条一直显示。

 

转载于:https://www.cnblogs.com/hw140430/p/4106269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值