Skip to content

Instantly share code, notes, and snippets.

@taktamur
Created July 9, 2014 16:08
Show Gist options
  • Save taktamur/177f3837298efcd3a452 to your computer and use it in GitHub Desktop.
Save taktamur/177f3837298efcd3a452 to your computer and use it in GitHub Desktop.
UIButtonで、画像とタイトルの位置を入れ替える時にハマった話 (UIEdgeInsets) ref: http://qiita.com/paming/items/a3b61dff825070f8ef40
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// ボタンの画像とtitleを入れ替え
// ButtonAは入れ替えない。
NSLog(@"self.buttonA.titleLabel=%@",self.buttonA.titleLabel);
NSLog(@"self.buttonA.imageView=%@",self.buttonA.imageView);
// ButtonBは、「移動させる方向に-1*サイズ分」設定してみる
CGFloat titleWidthB = self.buttonB.titleLabel.bounds.size.width;
CGFloat imageWidthB = self.buttonB.imageView.bounds.size.width;
self.buttonB.titleEdgeInsets=UIEdgeInsetsMake(0,
-imageWidthB,
0,
0);
self.buttonB.imageEdgeInsets=UIEdgeInsetsMake(0,
0,
0,
-titleWidthB);
NSLog(@"self.buttonB.titleLabel=%@",self.buttonB.titleLabel);
NSLog(@"self.buttonB.imageView=%@",self.buttonB.imageView);
// ButtonCは「移動させる方向に-1*サイズ分、反対方向にサイズ分」設定してみる
CGFloat titleWidthC = self.buttonC.titleLabel.bounds.size.width;
CGFloat imageWidthC = self.buttonC.imageView.bounds.size.width;
self.buttonC.titleEdgeInsets=UIEdgeInsetsMake(0,
-imageWidthC,
0,
imageWidthC);
self.buttonC.imageEdgeInsets=UIEdgeInsetsMake(0,
titleWidthC,
0,
-titleWidthC);
NSLog(@"self.buttonC.titleLabel=%@",self.buttonC.titleLabel);
NSLog(@"self.buttonC.imageView=%@",self.buttonC.imageView);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.viewA = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
self.viewB = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 100, 30)];
self.viewC = [[UIView alloc]initWithFrame:CGRectMake(0, 60, 100, 30)];
self.viewA.backgroundColor = [UIColor yellowColor];
self.viewB.backgroundColor = [UIColor redColor];
self.viewC.backgroundColor = [UIColor greenColor];
self.labelA = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 100, 30)];
self.labelB = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, 100, 30)];
self.labelC = [[UILabel alloc]initWithFrame:CGRectMake(0, 160, 100, 30)];
self.labelA.backgroundColor = [UIColor yellowColor];
self.labelB.backgroundColor = [UIColor redColor];
self.labelC.backgroundColor = [UIColor greenColor];
self.labelA.text = @"labelA";
self.labelB.text = @"labelB";
self.labelC.text = @"labelC";
self.labelA.textAlignment = NSTextAlignmentCenter;
self.labelB.textAlignment = NSTextAlignmentCenter;
self.labelC.textAlignment = NSTextAlignmentCenter;
self.buttonA = [UIButton buttonWithType:UIButtonTypeCustom];
self.buttonB = [UIButton buttonWithType:UIButtonTypeCustom];
self.buttonC = [UIButton buttonWithType:UIButtonTypeCustom];
self.buttonA.frame = CGRectMake(0, 200, 100, 30);
self.buttonB.frame = CGRectMake(0, 230, 100, 30);
self.buttonC.frame = CGRectMake(0, 260, 100, 30);
self.buttonA.titleLabel.backgroundColor = [UIColor yellowColor];
self.buttonB.titleLabel.backgroundColor = [UIColor redColor];
self.buttonC.titleLabel.backgroundColor = [UIColor greenColor];
[self.buttonA setTitle:@"buttonA" forState:UIControlStateNormal];
[self.buttonB setTitle:@"buttonB" forState:UIControlStateNormal];
[self.buttonC setTitle:@"buttonC" forState:UIControlStateNormal];
[self.buttonA setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.buttonB setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.buttonC setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:self.viewA];
[self.view addSubview:self.viewB];
[self.view addSubview:self.viewC];
[self.view addSubview:self.labelA];
[self.view addSubview:self.labelB];
[self.view addSubview:self.labelC];
[self.view addSubview:self.buttonA];
[self.view addSubview:self.buttonB];
[self.view addSubview:self.buttonC];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// View
// ViewAはそのまま
// ViewBは「右に-100」
self.viewB.frame = UIEdgeInsetsInsetRect(self.viewB.frame,
UIEdgeInsetsMake(0, 0, 0, -100));
// ViewCは「右に-100」「左に100」
self.viewC.frame = UIEdgeInsetsInsetRect(self.viewC.frame,
UIEdgeInsetsMake(0, 100, 0, -100));
// label
// labelAはそのまま
// labelBは「右に-100」
self.labelB.frame = UIEdgeInsetsInsetRect(self.labelB.frame,
UIEdgeInsetsMake(0, 0, 0, -100));
// labelCは「右に-100」「左に100」
self.labelC.frame = UIEdgeInsetsInsetRect(self.labelC.frame,
UIEdgeInsetsMake(0, 100, 0, -100));
// Button
// buttonB.titleは「右に-100」
self.buttonB.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -100);
// buttonC.titleは「右に-100」
self.buttonC.titleEdgeInsets = UIEdgeInsetsMake(0, 100, 0, -100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment