简体   繁体   English

UIButton设置框架不起作用?

[英]UIButton setting frame not working?

I have written the following code for my app: 我已经为我的应用编写了以下代码:

self.PlayButton = [[UIButton alloc] init];

//Setup Play Button
[self.PlayButton setTranslatesAutoresizingMaskIntoConstraints:NO];
self.PlayButton.frame = CGRectMake(self.view.frame.size.width * 0.38, self.view.frame.size.height * 0.666, self.view.frame.size.width * 0.48, self.view.frame.size.height * 0.29);
[self.PlayButton setBackgroundImage:[UIImage imageNamed:@"PlayButton.png"] forState:UIControlStateNormal];
[self.PlayButton.layer setMagnificationFilter:kCAFilterNearest];
[self.PlayButton addTarget:self action:@selector(PlayButtonMethod) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside]


[self.view addSubview:self.PlayButton];

But when this code it run rather than the image appearing in a rather specific location it simply appears in the top left hand corner of the view. 但是,当运行此代码时,而不是图像出现在特定位置时,它只会出现在视图的左上角。 Almost like it had been set 几乎像已经设定

self.PlayButton.frame = CGRectMake(0, 0, self.view.frame.size.width * 0.48, self.view.frame.size.height * 0.29);

It's odd because the width and height is put in correctly but for whatever reason the position of the button set by the CGRectMake is not taken into account. 奇怪的是,宽度和高度输入正确,但是由于任何原因,都没有考虑CGRectMake设置的按钮位置。 Ive done a bit of research into creating UIButtons in code and fro what I've seen not only has this happened to no-one else the code have written is perfectly liable. 我已经进行了一些研究,以代码形式创建UIButton,而我所看到的不仅是没有发生这种情况,而且代码编写的代码是完全负责任的。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

Looks like ive found the fix: Removing this line of code fixed the issue for me. 好像IVE找到了解决方法:删除此行代码为我解决了这个问题。

[self.PlayButton setTranslatesAutoresizingMaskIntoConstraints:NO];

not 100% sure why this was causing the problem but it fixed it. 不能100%确定这是为什么导致此问题,但已解决。

When creating UIButton s using the standard alloc init method doesn't actually create a button. 使用标准alloc init方法创建UIButton实际上并没有创建按钮。 The method for creating and initializing a UIButton is called buttonWithType: . 用于创建和初始化UIButton方法称为buttonWithType: This creates a UIButton of the specified type if you use alloc init it will not work correctly, see the Apple Documentation for UIButton . 如果使用alloc init这将创建指定类型的UIButton ,它将无法正常工作,请参阅Apple文档UIButton

So you need to change line 所以你需要换线

self.PlayButton = [[UIButton alloc] init];

to

self.PlayButton = [UIButton buttonWithType:UIButtonTypeCustom];

buttonWithType: allows you to pass in an enum of UIButtonType so it will accept any of the following values : buttonWithType:允许您传入UIButtonType的枚举,因此它将接受以下任何值:

  • UIButtonTypeCustom
  • UIButtonTypeSystem
  • UIButtonTypeDetailDisclosure
  • UIButtonTypeInfoLight
  • UIButtonTypeInfoDark
  • UIButtonTypeContactAdd
  • UIButtonTypeRoundedRect

If you don't pass it a UIButtonType the button will not be initialized as it will not know what type of button you want and it will not assume. 如果您不传递给它UIButtonType该按钮将不会初始化,因为它将不知道您想要的按钮类型,并且也不会假设该按钮。 Also check out the Apple Documentation for UIButtonType 另请查看Apple文档中的UIButtonType

Side Note 边注

I'm just going to include this as a side note. 我将把它作为附带说明。 Also have a read of the Apple Coding Conventions Documentation as I have noticed that you are using uppercase to start your variable names (ie PlayButton ). 也请阅读Apple Coding Conventions文档,因为我注意到您使用大写字母来启动变量名(即PlayButton )。 Variable names should start with lowercase (ie playButton ) and Class and Enum names should start with uppercase (ie UIViewController ). 变量名称应以小写字母(即playButton )开头,而类和枚举名称应以大写字母(即UIViewController )开头。 It's always good to stick to conventions as it makes you code more readable and maintainable so if another developer comes to modify your code it is easy for them or even yourself to read. 坚持约定总是好事,因为它使您的代码更具可读性和可维护性,因此,如果另一个开发人员来修改您的代码,则对它们甚至您自己都易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM