在所有视图控制器中添加自定义水平滚动视图

时间:2015-07-13 06:56:39

标签: ios scrollview viewcontroller appdelegate

我正在使用带有一些按钮和搜索栏的水平scrollview。我需要在所有视图控制器中显示它。这是我的代码

for (int i=0;i<[[[xmlSubDictionary objectForKey:@"pillars"]objectForKey:@"pillar"]count];i++) 
{
  [pillarArray addObject:[[[[[xmlSubDictionary objectForKey:@"pillars"]objectForKey:@"pillar"]objectAtIndex:i]objectForKey:@"pillarName"]objectForKey:@"text"]];

 ///// UIButton* ideaButton = [[UIButton alloc] initWithFrame:CGRectMake((i*self.scrolView.frame.size.width)/2.5, 3,     self.scrolView.frame.size.width/2.5,40)]; 
  if (i==0)
  { 
    ideaButton.frame = CGRectMake((i*self.scrolView.frame.size.width)/2.5, 3,   self.scrolView.frame.size.width/2.5, 40); 
    [self.view bringSubviewToFront:self.scrolView];

    [ideaButton setTitleColor:[UIColor colorWithRed:0 green:175 blue:69 alpha:1] forState:UIControlStateNormal]; 
  } 
 else
 { 
    [ideaButton setTitleColor:[UIColor colorWithRed:0.42 green:0.42 blue:0.42 alpha:1] forState:UIControlStateNormal]; 
 } 
 [ideaButton setTitle:[[pillarArray objectAtIndex:i] uppercaseString] forState:UIControlStateNormal]; 
 [ideaButton setTag:101+i];    
 [ideaButton addTarget:self action:@selector(ActionForMealButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
  ideaButton.titleLabel.font = [UIFont fontWithName:@"Akkurat-Bold" size:17.0];
  ideaButton.backgroundColor = [UIColor clearColor]; 
  [self.scrolView addSubview:ideaButton]; 
 }
 [self.scrolView setContentSize:CGSizeMake([pillarArray count] * ((self.view.frame.size.width+200)/([pillarArray count])-5), CGRectGetHeight(self.scrolView.frame))];

1 个答案:

答案 0 :(得分:0)

您可以创建UIView的新子类并在其中添加您的特定代码。在子类化时,您可以轻松地在所有视图控制器中使用视图。

UIView文档: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html