所以我试图制作4个按钮,每个按钮占据屏幕的1/4。但是,当我创建如下所示的按钮时,它们都处于相同的位置。我不确定这是怎么可能的,因为我已经在各个不同的地方设定了它们。有人可以解释为什么会发生这种情况并提供解决方案吗?
编辑:我删除了自动布局,但没有解决问题
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.sequence = [[NSMutableArray alloc] init];
float sizeWidth = [self view].bounds.size.width/2;
float sizeHeight = [self view].bounds.size.height/2;
CGPoint origin = [self view].bounds.origin;
// Configure button sizes
CGRect topLeft = CGRectMake(origin.x/2, origin.y/2, sizeWidth, sizeHeight);
_zeroButton.frame = topLeft;
CGRect topRight = CGRectMake(origin.x * 3/2, origin.y/2, sizeWidth, sizeHeight);
_oneButton.frame = topRight;
CGRect bottomLeft = CGRectMake(origin.x/2, origin.y * 3/2, sizeWidth, sizeHeight);
_twoButton.frame = bottomLeft;
CGRect bottomRight = CGRectMake(origin.x * 3/2, origin.y * 3/2, sizeWidth, sizeHeight);
_threeButton.frame = bottomRight;
}
答案 0 :(得分:0)
你所有的矩形都在原点0,0,因为你正在使用(仅)乘法和除法运算...... 你应该看看CGRectOffset().. 例如
CGRect rct = self.view.bounds;
rct.size.width /= 2.0;
rct.size.height /= 2.0;
UIButton *button1 = [[UIButton alloc]initWithFrame:rct]; //top left
// add target, addToView etc..
UIButton *button2 = [[UIButton alloc]initWithFrame: CGRectOffset(rct, rct.size.width, 0.0) ];
// etc..
UIButton *button3 = [[UIButton alloc]initWithFrame: CGRectOffset(rct, 0.0, rct.size.height) ];
//etc
UIButton *button4 = [[UIButton alloc]initWithFrame: CGRectOffset(rct, rct.size.width, rct.size.height) ];
//etc