Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton标题插图_Ios_Uibutton - Fatal编程技术网

Ios UIButton标题插图

Ios UIButton标题插图,ios,uibutton,Ios,Uibutton,标题用于对齐按钮上的标题和图片的插图。我在左边做了图片,在右边做了标题。但是当我点击按钮时,标题正在向左移动。多谢各位 UIImage *image = [UIImage imageNamed:imageName]; [self setImage:image forState:UIControlStateNormal]; CGSize imageSize = self.imageView.frame.size; CGSize titleSize = self.titleLabel.fr

标题用于对齐按钮上的标题和图片的插图。我在左边做了图片,在右边做了标题。但是当我点击按钮时,标题正在向左移动。多谢各位

UIImage *image = [UIImage imageNamed:imageName];
    [self setImage:image forState:UIControlStateNormal];

CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;

self.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
self.titleEdgeInsets = UIEdgeInsetsMake(0, ((self.frame.size.width-titleSize.width)/2)-imageSize.width, 0, 0);
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;


这是另一种直接设置标题和图像的方法-->:) 这样试试看,



可能是因为您仅将图像设置为“UIControlStateNormal”吗?我发现边缘插入的行为非常违反直觉,所以我创建了一个小演示应用程序,您可以使用它
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
[aButton setImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
[aButton setTitle:@"Rate us on app store" forState:UIControlStateNormal];
[aButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 50, 2, 20)];//set ur title insects also
[aButton setImageEdgeInsets:UIEdgeInsetsMake(2, -200, 2, 2)];//make negative edge for left side
[aButton setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:aButton];