ios UIButton设置单选效果,以及同时设置图片和标题

一,设置单选效果

- (void)selectedBtnPress:(UIButton*)sender

{

    //首先把原来按钮的选中效果消除

    for (int i=0;i<num;i++) {//num为总共设置单选效果按钮的数目

        UIButton *btn = (UIButton*)[view viewWithTag:i];//view为这些btn的父视图

        btn.selected = NO;

    }

    sender.selected = YES;//sender.selected = !sender.selected;

}

二 UIButton同时设置图片和标题

//UIEdgeInsetsMake(top.left.bottom,right)里面的四个参数表示距离上边界、左边界、下边界、右边界的距离,默认都为零,titleimagebutton的正中央

//图在左侧,标题在右侧

    UIButton *oneBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    oneBtn.frame = CGRectMake(50, 50, 150, 50);

    oneBtn.backgroundColor = [UIColoryellowColor];

    [oneBtn setImage:[UIImageimageNamed:@"0.gif"] forState:UIControlStateNormal];

    oneBtn.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 100);

    [oneBtn setBackgroundImage: [UIImageimageNamed:@"修改口味.png"] forState:UIControlStateNormal];

    [oneBtn setTitle:@"按钮"forState:UIControlStateNormal];

    oneBtn.titleEdgeInsets = UIEdgeInsetsMake(5, 50, 10, 10);

    [self.view addSubview: oneBtn];

 

二 避免同一界面里的两个UIButton同时被选中,触发action事件

    button.exclusiveTouch = YES;

原文地址:https://www.cnblogs.com/shidaying/p/3593783.html