Friday, August 16, 2013

Auto Adjusting UIButton On a UIView

Here is a simple requirement
- You can have any number of UIButtons say from 2 to 5, you need to adjust them in the horizontal view with each tags

2 Buttons Example :



5 Buttons Example :


 //  
 // BGGridButtonsView.h  
 //  
 // Created by Bishal Ghimire on 8/16/13.  
 // Copyright (c) 2013 Bishal Ghimire. All rights reserved.  
 //  
 #import <UIKit/UIKit.h>  
 @class BGGridButtonsView;  
 @protocol BGGridButtonsViewDelegate <NSObject>  
 -(void) selectedButton:(int)buttonNo forTagNo:(int)tagNo;  
 @end  
 @interface BGGridButtonsView : UIView  
 @property (nonatomic, strong) NSArray *buttonNames;  
 @property(assign) id <BGGridButtonsViewDelegate> delegate;  
 @end  


 //  
 // BGGridButtonsView.m  
 // Created by Bishal Ghimire on 8/16/13.  
 // Copyright (c) 2013 Bishal Ghimire. All rights reserved.  
 //  
 #import "BGGridButtonsView.h"  
 @implementation BGGridButtonsView  
 @synthesize buttonNames;  
 -(void) baseInit {  
   int count = [buttonNames count];  
   CGFloat padding = 10;  
   CGFloat width = self.frame.size.width / count - padding;  
   for (int i = 0; i < count; i++) {  
     CGFloat ptX = i * (width + padding) ;  
     UIButton *button = [self addMyButton:i+1];  
     button.frame = CGRectMake(ptX, 0, width, 20);  
     [self addSubview:button];  
   }  
 }  
 -(void) buttonAction:(UIButton *) sender {  
   DLog(@"%d", sender.tag);  
 }  
 - (UIButton *) addMyButton:(int)tagNo  
 {  
   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
   [button setTitle:buttonNames[tagNo-1] forState:UIControlStateNormal];  
   button.backgroundColor = [UIColor clearColor];  
   [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];  
   //UIImage *buttonImageNormal = [UIImage imageNamed:@""];  
   UIImage *buttonImageNormal = [UIImage imageNamed:@""];  
   UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:2 topCapHeight:2];  
   [button setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];  
   //  UIImage *buttonImagePressed = [UIImage imageNamed:@""];  
   //  UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:2 topCapHeight:2];  
   //  [button setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];  
   [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];  
   button.tag = tagNo;  
   [self addSubview:button];  
   return button;  
 }  
 - (id)initWithFrame:(CGRect)frame  
 {  
   self = [super initWithFrame:frame];  
   if (self) {  
   }  
   return self;  
 }  
 - (void)drawRect:(CGRect)rect  
 {  
   [self baseInit];  
 }  
 @end  

Finally On your View Controller
 #import "BGGridButtonsView.h"  
 @interface TestVC : UIViewController  
  <BGGridButtonsViewDelegate>  

 -(void) viewDidAppear:(BOOL)animated   {  
   BGGridButtonsView *buttonsGrid = [[BGGridButtonsView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];  
   buttonsGrid.buttonNames = @[ @"Call", @"PMS"];  
   buttonsGrid.tag = 1;  
   buttonsGrid.backgroundColor = [UIColor redColor];  
   [self.view addSubview:buttonsGrid];  
 }  

 #pragma mark Delegate Method  
 -(void) selectedButton:(int)buttonNo forTagNo:(int)tagNo {  
   DLog(@"%d , %d",buttonNo, tagNo);  
 }  

No comments:

Post a Comment