【问题标题】:how to make UIView work as an scrollview如何使 UIView 作为滚动视图工作
【发布时间】:2016-11-25 20:55:04
【问题描述】:

我需要一些认真的帮助来解决我的这个问题,我需要将UIView 作为ScrollView 工作。下面上传的图片将消除所有疑问。

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

【问题讨论】:

  • 你能展示你尝试过的代码吗
  • - (IBAction)login:(id)sender { self.title=@"LOGIN"; [_ScrollView setContentOffset:CGPointMake(0, 0) 动画:YES]; } - (IBAction)Register:(id)sender { self.title=@"REGISTER"; [_ScrollView setContentOffset:CGPointMake(_login.frame.size.width, 0) 动画:YES];这段代码来自我的另一个项目......在那个项目中,我使用了 2 个容器和一个滚动视图,在这 2 个按钮之上还有一个 UIView。我试图在这里应用相同的逻辑。但它没有发生

标签: ios objective-c uiview uiscrollview uicontainerview


【解决方案1】:

您好,您可以使用 UIScrollView 而不是 UIView,例如:

//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;

}

用于处理按钮操作。

-(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];
}

【讨论】:

  • 通过使用您的代码如何更改 ContainerView??
  • 是的,您必须使用滚动视图作为基础视图,然后通过调用 setupSegment 在滚动视图上添加按钮。如果你想在 cantainer 上进行自定义,然后在滚动视图上进行,想要在段上进行自定义,请在按钮上进行更改。
猜你喜欢
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多