如何使UIView作为滚动视图工作

我需要一些严重的帮助,我需要将UIView作为ScrollView 。 下载图片将清除所有的疑惑。

在这里输入图像说明

我已经采取了四个button以及他们下面的UIView以及4个容器视图,我已经把一个在另一个之上,以便与四个button一起工作..我现在想要的是将UIView作为ScrollView 。 每当我点击任何button,视图向前移动以及它改变容器视图。 每当我点击一个button,相应的容器视图。

你好,你可以使用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; } 

而对于Handelingbutton行动。

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