2016-07-22 47 views
1

我需要我的這個問題,我需要工作UIViewScrollView一些嚴重的幫助。在上傳的圖片下面會清除所有的疑惑。如何使UIView的工作作爲一個滾動視圖

enter image description here

我已經採取了四個按鈕與下面他們UIView與我已經把另一個之上的4點容器意見一起,以便隨着四個按鈕工作..我現在想要的是工作UIView作爲ScrollView。每當我點擊任何按鈕,視圖就會向前移動,並且它會更改容器視圖。每當我點擊一個按鈕時,相應的容器視圖就會顯示

+0

可以顯示烏爾試圖代碼 –

+0

- (IBAction爲)登錄:(ID)發送方{ self.title = @ 「LOGIN」; [_ScrollView setContentOffset:CGPointMake(0,0)的動畫:YES]; } - (IBAction爲)寄存器:(ID)發送方{ self.title = @ 「REGISTER」; [_ScrollView setContentOffset:CGPointMake(_login.frame.size.width,0)動畫:YES]; } 此代碼是從我的其他項目......在這個項目我已經使用2個容器連同滾動視圖及以上,與一個UIView 2buttons一起。我試圖在這裏應用相同的邏輯。但它沒有發生 – pri

+0

???????????????? – pri

回答

3

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

//Declare it 
NSMutableArray * buttonArray 
int selectedIndex; 


-(void)setUpSegmentBar { 

    NSArray * namesOfMenus [email protected][@"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; 

} 

和HANDELING按鈕操作。

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

通過使用你的代碼我如何改變ContainerView? – pri

+0

是的,你必須使用滾動視圖作爲基本視圖,然後bycalling setupSegment你滾動視圖添加按鈕。如果你想對cantainer進行定製,那麼在滾動視圖上執行並且想要在定製按鈕上進行定製。 –

+0

@pri如果它能解決您的問題,請做出正確的決定。 –