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);  
 }  

Tuesday, August 13, 2013

Git setup on Mac, Linux or Unix using Terminal


Git global setup:

git config --global user.name "Bishal Ghimire"
git config --global user.email  "you@ youremail.com"

Create Repository

mkdir yourProject
cd yourProject
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@git.youremail.com: yourProject.git
git push -u origin master


Existing Git Repo?
cd existing_git_repo
git remote add origin git@git. youremail.com:yourProject.git
git push -u origin master


That is it ... 

Bonus Codes

Force Push Local to server

git push origin master --force


Force Pull From Server to local



# fetch from the default remote, origin
git fetch
# reset your current branch (master) to origin's master
git reset --hard origin/master



Branch and Merge




# fetch from the default remote, origin
git fetch
# create a branch at your current master
git branch old-master
# reset to origin's master
git reset --hard origin/master
# merge your old master, keeping "our" (origin/master's) content
git merge -s ours old-master

Common Git Issues And Solutions 


Issue 1 : "File Locked, unable to edit in XCode"

Solution : Change File Read Write permission 

As you have made force changes from the file from internet you are more likely to lock the files, which XCode will not allow you to edit all you can do is read the files only !
To chage that on your root folder of your terminal

sudo chmod -R 0777 ./*


Issue 2 : “fatal: remote origin already exists”


Solution :
Other 'origin' alreadt exists on remote server or is already on use

git remote rm origin
git remote add origin git@youremail.github.com:youriOSapp/my_app.git
git push -u origin --all # pushes up the repo and its refs for the first time


resync-git-repo-with-new-gitignore-file


git rm -r --cached .
git add .git commit -m ".gitignore is now working"

git-mergetool

git-mergetool - Run merge conflict resolution tools to resolve merge conflicts
Syntax - 


git mergetool [--tool=<tool>] [-y | --[no-]prompt] [<file>...]
Eg -
git mergtool --tool=emerge

Keyboard Tips 
n =  next
a / b = alter version

Read More At :

http://www.mac-terminal.com/files-and-folders/permissions/chmod/
http://stackoverflow.com/a/4787356/1294448
http://stackoverflow.com/questions/10904339/github-fatal-remote-origin-already-exists



Friday, August 9, 2013

Create Grid Layout View Matrix Programatically

OutPut




View Controller

 #import "BGGridImagesView.h"  
 -(void) groupLayout {  
   BGGridImagesView *gridView = [[BGGridImagesView alloc] initWithFrame:CGRectMake(0, 0, 140, 100)];  
   [self.view addSubview:gridView];  
 }  


View
BGGridImagesView.h
 #import <UIKit/UIKit.h>  
 @interface BGGridImagesView : UIView  
 @end  


BGGridImagesView.m
 //  
 // BGGridImagesView.m  
 //  
 // Created by Bishal Ghimire on 8/9/13.  
 // Copyright (c) 2013. All rights reserved.  
 //  
 #import "BGGridImagesView.h"  
 @implementation BGGridImagesView  
 - (id)initWithFrame:(CGRect)frame  
 {  
   self = [super initWithFrame:frame];  
   if (self) {  
     [self layoutGrid];  
   }  
   return self;   
 }   
 -(void) layoutGrid {  
   NSInteger grid[10][10];  
   NSInteger k = 0;  
   for (int row = 0; row < 4; row++) {  
     for (int column = 0; column < 5; column++) {  
       k++;  
       grid[row][column] = k;  
       UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(column*30, row * 30, 20, 20)];  
       NSString *string = [[NSString alloc] init];  
       string = [NSString stringWithFormat:@"%d",k];  
       lbl.text = string;  
       [self addSubview:lbl];  
     }  
   }  
 }  
 /*  
 // Only override drawRect: if you perform custom drawing.  
 // An empty implementation adversely affects performance during animation.  
 - (void)drawRect:(CGRect)rect  
 {  
   // Drawing code  
 }  
 */  
 @end  



Using UIButtons
 #pragma mark - Grid Layout  
 #define FACTOR 1.0  
 #define kHeight 85.0f  
 #define kWidth 85.0f  
 #define xPadding 15.0f  
 #define yPadding 25.0f  
 #define kNoOfColumn 3  
 -(void) loadGridImages {  
   UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];  
   int numberOfImages = 30;  
   CGRect frame = self.frame;  
   NSInteger grid[30][30];  
   NSInteger k = 0;  
   BOOL dobreak = NO;  
   CGFloat heightFactor = ( kHeight / kNoOfColumn ) * 3;  
   CGFloat widthFactor = ( kWidth / kNoOfColumn ) * 3;  
   for (int row = 0; row < (numberOfImages / kNoOfColumn) && !dobreak; row++) {  
     for (int column = 0; column < kNoOfColumn; column++) {  
       k++;  
       grid[row][column] = k;  
       DLog(@"%d",k);  
       if (k > numberOfImages) {  
         dobreak = YES;  
         break;  
       }  
       frame.size.height = heightFactor;  
       frame.size.width = widthFactor;  
       frame.origin.x = 10 + ( heightFactor * column) + (10 * column);  
       frame.origin.y = 20 + ( widthFactor * row) + (10 * row);  
       UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
       NSString *string = [NSString stringWithFormat:@"%d",k];  
       [button setTitle:string forState:UIControlStateNormal];  
       button.titleLabel.font = [UIFont systemFontOfSize:30];  
       //button.backgroundColor = [UIColor blueColor];  
       [button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];  
       button.frame = frame;  
       button.tag = k;  
       [scrollView addSubview:button];  
     }  
   }  
   [scrollView setContentSize:CGSizeMake(self.frame.size.width - xPadding, (((numberOfImages+1)/2) * heightFactor) * 0.8)];  
   [self addSubview:scrollView];  
 }  

Alternate Code* 
 #define FACTOR 1.0  
 #define kHeight 85.0f  
 #define kWidth 85.0f  
 #define xPadding 15.0f  
 #define yPadding 25.0f  
   
 // Three Grid Layout  
   
 -(void) loadGridImages{  
   int numberOfImages = 9;  
     
   CGRect frame = self.scrollView.frame;  
   for (int i = 0; i < numberOfImages; i++) {  
     int j = 0;  
     if (i < 3) {  
       j = i+3;  
     } else {  
       j = i;  
     }  
       
     if (j%3 == 0 ) {  
       frame.origin.x = xPadding;  
     }  
     else if ((j-1)%3 == 0 ) {  
       frame.origin.x = xPadding * 2 + kWidth * 1 ;  
     } else if ((j+1) %3 == 0) {  
       frame.origin.x = xPadding * 3 + kWidth * 2;  
     }  
       
     // frame.origin.y = ((i/3) * kHeight) + yPadding * 3;  
     frame.origin.y = ((i/3) * kHeight) + yPadding * (i/3);  
   
     frame.size.height = kHeight * FACTOR;  
     frame.size.width = kWidth * FACTOR;  
       
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
     UIImage *myImage = [UIImage imageNamed:@"checkmark"];  
     [button setBackgroundImage:myImage forState:UIControlStateNormal];  
       
     [button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];  
     button.frame = frame;  
     button.tag = i+1;  
        
     [self.scrollView addSubview:button];  
   }  
 }  

*( NOT A Good algorithm ) but just for example



Finally to layout codes with format in Blogger I used -
http://codeformatter.blogspot.com/

Sunday, August 4, 2013

Discover and organize Github alternate repositories

Do you use lots of framework in your project. If so you must be using Cocoapods.
But what if you want to find alternate solution to your current framework .

Try Gitrep.

For example if you want to try RestKit as framework but explore alternate framework then
go to this link
http://gitrep.com/users/RestKit/repos/RestKit
It will show u other similar alternate framework.

But if you want to learn RestKit follow this stackover flow answer.

And for facebook's popular framework three20 - which no longer supported has alternate nimbus

http://gitrep.com/users/facebook/repos/three20

http://gitrep.com/

Happy Discovering :)

Xcode plug-in to autocomplete name for image and Color



Can't remember whether that image you just added to the project was called button-separator-left or button-left-separator? Now you don't have to, because this will autocomplete your imageNamed: calls like you'd expect. Just type in [ NSImage imageNamed: or [ UIImage imageNamed: and all the images in your project will conveniently appear in the autocomplete menu.




Monday, June 17, 2013

Facebook iOS app for iOS7 without left side bar !

If you have used or seen the new iOS 7, then there is one risky think apple has added to the OS. That is system wide swipe gesture ie swipe left to go back ! Why is it so risky ? It is because when we left swipe most of the app will open the side menu ! The example is facebook !!!

Now lets look at the new Facebook app for ios 7 which easily solves the slide side view menu problem, and goes native to iOS with tab bar controllers !

But this is only a test UI not a final UI hence it looks like a quick and dirty fix rather then a final flat iOS7 style design. Lets say its a functional design rather then graphical one.






Monday, January 21, 2013

How to install the Mac App Store in OSX 10.6 Snow Leopard

Step 1 : Goto

http://support.apple.com/kb/DL1399

Step 2 : Download the dmg file

Download

Step 3: Close Xcode or any-other system software and Browsers like Chrome or Safari

Step 4 : You are ready to go using app store after installing and dont forget to restart your machine

Note that it is a big download, file size is 1Gb. So it will take time.


About Mac OS X 10.6.8 Update Combo v1.1

The 10.6.8 update is recommended for all users running Mac OS X Snow Leopard and includes general operating system fixes that enhance the stability, compatibility, and security of your Mac, including fixes that:
  • Enhance the Mac App Store to get your Mac ready to upgrade to Mac OS X Lion
  • Resolve an issue that may cause Preview to unexpectedly quit
  • Improve support for IPv6
  • Improve VPN reliability
  • Identify and remove known variants of Mac Defender
SHA1 = e0742e7537f80c9e01376ea3b3c09df993900643

  • Version: 10.6.8 v.1.1
  • Post Date: Jul 25, 2011
  • Download ID: DL1399
  • License: Update
  • File Size: 1.09 GB
System Requirements
Mac OS X v10.6 - Mac OS X 10.6.7


BONUS
Now you have Mac App Store installed you are ready to go and install Mac OS X 10.8 on your system

Step 5 : Now as you navigate to
http://www.apple.com/osx/
And click "Upgrade Now"
Upgrade Now

Or

https://itunes.apple.com/us/app/os-x-mountain-lion/id537386512?ls=1&mt=12



via - Source