3

我遇到了关于 UIScrollView 的问题。我正在制作一个继承 UIView 的自定义视图。该视图有一个 UIScrollView,上面有很多应该左右滚动的按钮。UIScrollView 和按钮可以正常显示。但我无法滚动按钮。有人可以给我一些建议吗?非常感谢!

MZMPhotoCalenderSwitcher.h

#import <UIKit/UIKit.h>

@interface MZMPhotoCalenderSwitcher : UIView <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *topSwitcher;

@end

MZMPhotoCalenderSwitcher.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];
    self.topSwitcher.backgroundColor = [UIColor greenColor];
    self.topSwitcher.pagingEnabled = YES;
    self.topSwitcher.showsHorizontalScrollIndicator = NO;
    self.topSwitcher.showsVerticalScrollIndicator = NO;

    [self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f];
}

- (void)add:(int)num ButtonsOnView:(UIScrollView *)view withButtonWidth:(CGFloat)width andHeight:(CGFloat)height
{
    CGFloat totalTopSwitcherWidth = num * width;
    [view setContentSize:CGSizeMake(totalTopSwitcherWidth, view.bounds.size.height)];
    CGFloat xOffset = 0.0f;

    for (int i=1; i<=num; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setFrame:CGRectMake(xOffset, 0, width, height)];
        xOffset += width;
        [button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];

        button.titleLabel.font = [UIFont systemFontOfSize:10];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [button setTag:i];
        [button addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];

        if (i % 2 == 0)
            [button setBackgroundColor:[UIColor yellowColor]];
        else
            [button setBackgroundColor:[UIColor redColor]];

        [view addSubview:button];
    }
}
4

10 回答 10

5

下面一行

[view addSubview:button];

添加

view.contentSize = CGSizeMake(view.contentSize.width, button.frame.origin.y + button.frame.size.height);

这会将滚动视图的内容大小设置为按钮的底部。

于 2013-10-24T12:12:32.913 回答
1

I'm using Xcode 6/iOS 8+ now with auto-layout turned on, and I had the same problem in the first place, removing auto-layout doesn't work at all, so in order to make scroll view scrollable vertically, I made sure the following:

  1. The scroll view content size height MUST be bigger than the screen height, this almost goes without saying...

  2. The top/bottom/left/right constraint of the scroll view HAS TO BE pinned, I did this using storyboard so no code showing up here

If you want to make the scroll view scrollable horizontally, then make sure its content size width is bigger than the screen width, the other rule applies the same.

This worked for me, hope it can help someone.

于 2015-03-15T02:55:18.547 回答
1

应该是内容大小的问题,还要检查 UIScrollView 对象的 scrollEnabled 属性是否设置为 TRUE,

检查UIScrollView 的解决方案不会滚动!

于 2013-10-24T11:27:21.280 回答
1

只需在您的项目中禁用“自动布局”,它就会开始工作!

于 2013-10-25T04:32:15.010 回答
1

将所有这些建议组合成一个简单的答案:

// 在 vi​​ewDidLoad

// set the size of the scrollview (in this case I made it the size of its container
self.scrollView.frame = self.view.frame;
// now set the size of the content to be scrolled within the scrollview
// the content to be displayed must be bigger than the scrollView
// in this case I added 100 to make the content size, so that it is taller than the height of the scrollView
// now it scrolls
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height + 100);
于 2016-06-03T04:11:22.533 回答
0

你在 SuperVIew 上的哪里添加 ScrollView?你在哪里重新计算滚动视图的内容大小属性?

[view setContentSize:CGSizeMake(<summYourViews.width>,<summYourViews.height>)];
于 2013-10-24T11:14:47.047 回答
0

太奇妙了。

我只是将 viewDidLoad 中的代码移动到 initWithFrame:,然后它就可以工作了。我不知道为什么会这样?

于 2013-10-25T04:29:40.897 回答
0

尝试这个 。. . .

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];
    self.topSwitcher.backgroundColor = [UIColor greenColor];
    self.topSwitcher.pagingEnabled = YES;
    self.topSwitcher.showsHorizontalScrollIndicator = NO;
    self.topSwitcher.showsVerticalScrollIndicator = NO;
    [topSwitcher setContentSize:CGPointMake(self.view.bounds.size.width,TOP_SWITCHER_HEIGHT+400)];

    [self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f];
}
于 2013-10-25T03:54:44.587 回答
0

您必须设置滚动视图框架大小和内容大小。

于 2013-10-24T12:37:48.957 回答
0

您的问题是您尚未设置 contentSize,因此 contentSize 与框架视图相同。

您需要在 viewDidLoad 中执行此操作

- (void)viewDidLoad {
    ........
    self.scrollView.contentSize = CGSizeMake(/*yourWidth*/, /*yourHeight/*);
    ........
}

尝试将您的 contentSize 调整为对象的最后一个位置加上它的大小加上一个填充。

于 2013-10-24T12:00:53.273 回答