0

I am using a horizontal scrollview with some buttons and a search bar.I need to show it in all view controllers .Here is my code

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))];
user4261201
  • 2,324
  • 19
  • 26
RakeshBiswal
  • 59
  • 10

1 Answers1

0

You can create a new subclass of UIView and add your specific code in there. When subclassed, you can use your view in all your viewcontrollers easily.

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

knutigro
  • 1,082
  • 2
  • 10
  • 20
  • Clicking on button i need to call multiple service call multiple service call.It needs to place of at top of the controller exactly at the navigation bar.In my application there is no navigation bar only that that horizontal scroller consist multiple button and a search bar. – RakeshBiswal Jul 13 '15 at 08:17
  • In my projects I use lots of subclassed views but I try to keep all actions that should perform some further logic in the app to `delegate` calls. Your subclassed UIView should implement delegate logic that can call back on the viewcontroller when a button was clicked, and which button was clicked. – knutigro Jul 13 '15 at 08:22
  • yes...exactly...should i use scroll view xib for it? – RakeshBiswal Jul 13 '15 at 08:58
  • You can use xib, and using xib will make it easier for you to manage e.g. autolayout. On the contrary I often write simple UIViews only using code, - no xib, The reason for this is that I then can directly use them in storyboards. I then override `-(id)initWithCoder:(NSCoder *)aDecoder` for initilaisation. – knutigro Jul 13 '15 at 09:02