1添加一個可自由滑動的按鈕—–需要到入第三方

(需要導入兩個文件#import "UIButton+NMCategory.h”以及  .m)

UIButton *XZSBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    XZSBtn.backgroundColor = [UIColor navbarColor];
    [XZSBtn setImage:[UIImage imageNamed:@"helpXZS"] forState:UIControlStateNormal];
    XZSBtn.frame = CGRectMake(SCREEN_WIDTH-64, SCREEN_HEIGHT-200, 64, 64);
    XZSBtn.layer.masksToBounds=YES;
    XZSBtn.layer.cornerRadius = 32;
    [XZSBtn addTarget:self action:@selector(XZSClick:) forControlEvents:UIControlEventTouchUpInside];
    [XZSBtn setDragEnable:YES];//這兩句起作用的
  [XZSBtn setAdsorbEnable:YES];
    [self.view addSubview:XZSBtn];




-(void)XZSClick:(id)send
{
    ChatViewController *chat=[[ChatViewController alloc]init];

    [self.navigationController pushViewController:chat animated:YES];

}

2.可以自由拖拽 且判斷是否拖拽 或者點擊—不需要導入第三方

    //   可滑動的小助手
    UIButton *XZSBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    XZSBtn.backgroundColor = [UIColor navbarColor];
    [XZSBtn setImage:[UIImage imageNamed:@"helpXZS"] forState:UIControlStateNormal];
    XZSBtn.frame = CGRectMake(SCREEN_WIDTH-64, SCREEN_HEIGHT-200, 64, 64);
    XZSBtn.layer.masksToBounds=YES;
    XZSBtn.layer.cornerRadius = 32;
    [XZSBtn addTarget:self action:@selector(XZSClick:) forControlEvents:UIControlEventTouchUpInside];
    [XZSBtn addTarget:self action:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];
    a=0;
    [self.view addSubview:XZSBtn];
//點擊事件
-(void)XZSClick:(id)send
{
    if (a==0)
    {
        ChatViewController *chat=[[ChatViewController alloc]init];
        NSLog(@"跳到小助手頁面");
        [self.navigationController pushViewController:chat animated:YES];
    }
    a=0;

}
//拖拽狀態下的方法
- (void) dragMoving: (UIButton *)btn withEvent:(UIEvent *)event

{
    a=1;
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    CGFloat x = point.x;
//    CGFloat y = point.y;
    CGFloat btnx = btn.frame.size.width/2;
//    CGFloat btny = btn.frame.size.height/2;
    if(x<=btnx)
    {
        point.x = btnx;
    }
    if(x >= self.view.bounds.size.width - btnx)
    {
        point.x = self.view.bounds.size.width - btnx;
    }
    btn.center = point;
}

3.自定義的按鈕 有圖片+label

#import <UIKit/UIKit.h>

@interface regbutton : UIButton
-(void)setBtnImageName:(NSString*)imgName WithbtnTit:(NSString*)title;

-(void)setBtnImageName:(NSString*)imgName WithbtnTit:(NSString*)title
{
    UIImage *imag=[UIImage imageNamed:imgName];//如何獲取圖片本身大小
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, imag.size.width, imag.size.height)];
    img.image=[UIImage imageNamed:imgName];//這個必須有
    [self addSubview:img];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 20, 100, 20)];
    label.textColor = [UIColor colorWithHex:0x333333];
    label.text=title;//這個必須有
    label.font = [UIFont systemFontOfSize:19];
    [self addSubview:label];

}

4.循環創建按鈕進行點擊切換顏色問題

-(void)btn:(UIButton*)sender{
  for (UIButton *btn in _btnArray){
      if (btn.tag ==sender.tag) {
    [sender setTitleColor:MainRGB forState:UIControlStateNormal];
            } else {
    [sender setTitleColor:MainRGB forState:UIControlStateNormal];
            }
      }
 }

5.按鈕上字體位置

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft
分享