1. 程式人生 > >iOS Button 按鈕 設定圖片在文字的右邊

iOS Button 按鈕 設定圖片在文字的右邊

     UIButton * cityBtn=[UIButtonbuttonWithType:UIButtonTypeCustom];

    [cityBtn setTitle:@"上海"forState:UIControlStateNormal];

    [cityBtn setImage:[UIImageimageNamed:@"icon"]forState:UIControlStateNormal];

//最重要是下面兩行設定圖片與文字的間距

    cityBtn.titleEdgeInsets=UIEdgeInsetsMake(0, -70,0, 0);//上左下右

    cityBtn.imageEdgeInsets

=UIEdgeInsetsMake(13,30, 13, 30);

    cityBtn.frame=CGRectMake(60,0, 80, 40);

    [self.view addSubview:cityBtn];