Custom UIButton for Iphone

前端 未结 4 753
梦毁少年i
梦毁少年i 2020-12-23 12:49

I have an view in my App which has a number of buttons based on the number of items returned by the server. So if the server returns say 10 items, there should be 10 buttons

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 13:12

    you can use individual class for custom Roundrect button which can be useful in whole project with your specific frame style as below

    #import 
    #import 
    @interface CustomRoundRectButton : UIButton
    @end
    
    
    #import "CustomRoundRectButton.h"
    @implementation CustomRoundRectButton
    - (void)drawRect:(CGRect)rect
    {
    [[self layer] setMasksToBounds:YES];
    [self.layer setCornerRadius:10.0f]; 
    [self.layer setBorderColor:[UIColor grayColor].CGColor]; 
    [self.layer setBorderWidth:1.0];
    }
    @end
    

    In this you have to select button type custom and select its class as CustomRoundRectButton.

    For Simple custom button we can use as below
    
    -(UIBarButtonItem*)BackButton
    {
    UIButton*btn =  [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(0, 0, 30, 30)];
    [btn addTarget:self action:@selector(actionBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*barBtn = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
    return barBtn;
    }
    

提交回复
热议问题