如何使 UIView 作为滚动视图工作 [英] how to make UIView work as an scrollview

查看:37
本文介绍了如何使 UIView 作为滚动视图工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些严肃的帮助来解决我的这个问题,我需要将 UIView 用作 ScrollView.下面上传的图片会解开所有的疑惑.

I need some serious help on this problem of mine where I need to work a UIView as an ScrollView. Below uploaded picture will clear all the doubts.

我已经拿了四个按钮以及它们下面的 UIView 以及 4 个容器视图,我将它们放在另一个上面以便与四个按钮一起工作..我现在想要的是将 UIView 用作 ScrollView.每当我单击任何按钮时,视图都会向前移动并更改容器视图.每当我单击按钮时,就会显示相应的容器视图.

I have taken four buttons along with a UIView below them along with 4 container views which I have placed one above the other so as to work along with the four buttons.. What I want now is to work UIView as an ScrollView. Whenever I click any button, the View moves forward as well as the it changes the container view. And respective container view shows whenever I click a button.

推荐答案

你好,你可以使用 UIScrollView 而不是 UIView 像:

Hi better you can use UIScrollView instead of UIView like:

//Declare it 
NSMutableArray * buttonArray
int selectedIndex;


-(void)setUpSegmentBar {

    NSArray * namesOfMenus =@[@"Button1",@"Button2",@"Button3",@"Button4",@"Button5"];

    // Scrollview added on storyBoard
    myScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 2, self.view.frame.size.width, 40)];
    CGFloat scrollWidth = 0.f;

   buttonArray=[[NSMutableArray alloc]init];

    for ( int j=0; j<namesOfMenus.count; j++)
    {
        NSString * name =[namesOfMenus objectAtIndex:j];
        CGSize size = [name sizeWithAttributes:
                       @{NSFontAttributeName: [UIFont fontWithName:font_bold size:font_size_button]}];

        CGSize textSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
        CGFloat strikeWidth;

        if (iPAD) {
            strikeWidth = self.view.frame.size.width/5.5;
        }else{
            strikeWidth = textSize.width;
        }
        CGRect frame = CGRectMake(scrollWidth, 0,strikeWidth+20, 40);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTag:j];
        button.frame = frame;
        [button setBackgroundColor:[UIColor whiteColor]];
        button.titleLabel.textColor=[UIColor whiteColor];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.layer.borderColor=[UIColor whiteColor].CGColor;
        button.titleLabel.textAlignment=NSTextAlignmentCenter;
        [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:name forState:UIControlStateNormal];

        // ScrollWidth As per Button Width
        scrollWidth= scrollWidth+strikeWidth+20;

        // to Check Which Button Is Selected.
        if (j==selectedIndex) {
            // If Selected Do UI Changes
            button.backgroundColor=segment_selected_Color ;
            [button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
            [button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
            [button addShaddow];
            if (iPhone6||iPhone6plus) {
                button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
            }else {
                button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
            }
        }else {
            // Other Buttons
            [button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
            button.backgroundColor=segment_disselected_Color;
            [button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
            if (iPhone6||iPhone6plus) {
                button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
            }else{
                button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
            }
        }
        // Add Buttons in Array
        [buttonArray addObject:button];
        // Add button on Scroll View
        [myScrollView addSubview:button];

    }
    myScrollView.contentSize = CGSizeMake(scrollWidth, 30.f);
    myScrollView.pagingEnabled = NO;
    myScrollView.backgroundColor=[UIColor whiteColor];

    [myScrollView setShowsHorizontalScrollIndicator:NO];
    [myScrollView setShowsVerticalScrollIndicator:NO];
    return myScrollView;

}

以及用于处理按钮操作.

And For Handeling button action.

-(void)buttonEvent:(UIButton*)sender
{
    NSInteger index= sender.tag;
    selectedIndex= (int) index;

    for(int i=0;i<buttonArray.count;i++)
    {
        UIButton * button =(UIButton*)[buttonArray objectAtIndex:i];
        if (i==selectedIndex) {
            [button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
            button.backgroundColor=segment_selected_Color ;
            [button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
            [button addShaddow];
            if (iPhone6||iPhone6plus) {
                button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
            }else {
                button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
            }
        }else {
            button.backgroundColor=segment_disselected_Color;
            [button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
            [button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
            if (iPhone6||iPhone6plus) {
                button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
            }else{
                button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
            }
        }
    }

    CGRect frame1 = myScrollView.frame;
    UIButton * bt=(UIButton*)[buttonArray objectAtIndex:index];
    frame1 =bt.frame ;
// By using it button will scroll and show properly
    [myScrollView scrollRectToVisible:frame1 animated:YES];
    [self setupTableAfterClick];
}

这篇关于如何使 UIView 作为滚动视图工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆