【问题标题】:Is it possible to use UIScrollView in a UIView and not in a UIViewController?是否可以在 UIView 而不是 UIViewController 中使用 UIScrollView?
【发布时间】:2013-07-24 07:02:33
【问题描述】:

是否可以在 UIView 中使用 UIScrollView 而不是 UIViewController?

我尝试在 UIView 中使用,但这不起作用。我找不到适合我的项目的示例。

我需要在大小为 90x90 像素的视图上水平滚动。

这是我的代码

    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 35, 400, 200)];
    [scrollView setScrollEnabled:YES];
    scrollView.pagingEnabled = YES;
    [scrollView setDelegate:self];
    scrollView.backgroundColor = [UIColor brownColor];
    [scrollView setShowsHorizontalScrollIndicator:YES];
    [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    scrollView.contentMode = UIViewContentModeScaleToFill;
    [scrollView setContentSize:CGSizeMake(630,0)];

    [self addSubview:scrollView];


    //self.frame = CGRectMake(0, 0, 1024, 768);

    [self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];


    circleView = [[UIView alloc] initWithFrame:CGRectMake(100,100,90,90)];        
    circleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
    //circleView.layer.borderColor = [[UIColor blackColor] CGColor];
    //circleView.layer.borderWidth = 4;

    UILabel* circleIndex = [[UILabel alloc] init];
    circleIndex.frame    = CGRectMake(30, 25, 40, 40);
    [circleIndex setFont:[UIFont fontWithName:@"Arial-BoldMT" size:40]];
    [circleIndex setText:@"A"];

    [circleView addSubview:circleIndex];


    circleView2 = [[UIView alloc] initWithFrame:CGRectMake(100+100,100,90,90)];
    circleView2.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    circleView3 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100,100,90,90)];
    circleView3.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    circleView4 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100+100,100,90,90)];
    circleView4.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    circleView5 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100+100+100,100,90,90)];
    circleView5.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    circleView6 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100+100+100+100,100,90,90)];
    circleView6.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];

    [scrollView addSubview:circleView];

    [scrollView addSubview:circleView2];

    [scrollView addSubview:circleView3];

    [scrollView addSubview:circleView4];

    [scrollView addSubview:circleView5];

    [scrollView addSubview:circleView6];



    exitView = [[UIView alloc] initWithFrame:CGRectMake(70,-5,30,30)];
    exitView.layer.cornerRadius = 15;

    exitView.backgroundColor = [UIColor redColor];
    exitView.layer.borderColor = [[UIColor whiteColor] CGColor];
    exitView.layer.borderWidth = 2;

    [circleView addSubview:exitView];

    UIView *exitLabel = [[UIView alloc] initWithFrame:CGRectMake(8,13,15,3)];
    exitLabel.backgroundColor = [UIColor clearColor];
    exitLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
    exitLabel.layer.borderWidth = 2;


      [exitView addSubview:exitLabel];

    UILongPressGestureRecognizer *longPress =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(handleLongPress:)];
    [circleView addGestureRecognizer:longPress];

    UITapGestureRecognizer *singlePress =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(handleSinglePress:)];
    [exitView addGestureRecognizer:singlePress];
    //[longPress release];


    UITapGestureRecognizer *singlePress2 =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(singlePress:)];
    [circleView2 addGestureRecognizer:singlePress2];



    [exitView setHidden:YES];

    [circleView bringSubviewToFront:exitView];




    [exitView setUserInteractionEnabled:YES];


}
return self;
}

【问题讨论】:

  • 是的,这是可能的。请分享您的代码。
  • 请分享您的代码。
  • UIScrollViewUIView 的子类。任何 UIView 实例都可以是任何其他 UIView 的子视图。
  • 我已经添加了我的代码,我无法看到滚动效果
  • 为什么你的 contentSize 的高度为 0?

标签: ios objective-c uiview uiviewcontroller uiscrollview


【解决方案1】:

我按原样粘贴了你的代码,我也能看到滚动。
你的代码好像被粘贴到了
- (id)initWithFrame:(CGRect)frame
的自定义视图。
你为你的观点提供的框架是什么?

【讨论】:

    【解决方案2】:

    我认为的问题是内容大小没有正确定义

    [scrollView setContentSize:CGSizeMake(630,0)];
    

    宽度为 630,高度为 0 表示您尝试实现水平滚动。尝试一些更大的内容大小并尝试滚动代码看起来不错

    我尝试了一个带有滚动视图的uiview,效果很好

    @implementation TestView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            [self setBackgroundColor:[UIColor yellowColor]];
            UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 35, 600, 600)];
            [scrollView setScrollEnabled:YES];
            scrollView.pagingEnabled = YES;
            scrollView.backgroundColor = [UIColor blueColor];
            [scrollView setShowsHorizontalScrollIndicator:YES];
            [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            scrollView.contentMode = UIViewContentModeScaleToFill;
            [scrollView setContentSize:CGSizeMake(630,0)];
    
            [self addSubview:scrollView];
    
    
    
        }
        return self;
    }
    

    添加到 VC 中

    TestView *view=[[TestView alloc]initWithFrame:CGRectMake(20, 30, 500, 600)];
    
    [self.view addSubview:view];
    

    【讨论】:

    • 他提到他只需要水平滚动。这不是问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 2019-05-17
    相关资源
    最近更新 更多