Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.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 实现相对大小的按钮_Ios_Objective C_Xcode - Fatal编程技术网

Ios 实现相对大小的按钮

Ios 实现相对大小的按钮,ios,objective-c,xcode,Ios,Objective C,Xcode,所以我尝试制作4个按钮,每个按钮占据屏幕的1/4。但是,当我创建如下所示的按钮时,它们都处于相同的位置。我不确定这是怎么可能的,因为我把它们的起源设定在一个完全不同的地方。有人能解释为什么会发生这种情况并提供解决方案吗 编辑:我删除了自动布局,没有解决问题 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a ni

所以我尝试制作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,因为你(只)对它们使用乘法和除法运算。。。 您也许应该看看CGRectOffset()。。 例如


尝试像这样检查源代码:NSLog(@“%@”、NSStringFromCGPoint(源代码));在CGPoint原点=。。。你会明白你的错误的。是的,@stosha是对的。我想告诉你你的错误,但最好你自己看看。原点是{0,0}吗?如果是这样,那么您的操作将始终以{0,0}结束。明白了,伙计们,谢谢@blzn和通常的
边界
(与帧不同)将始终返回原点{0,0}。不过在本例中,视图的边界和帧都位于{0,0}。您可能需要使用屏幕高度和宽度来获得所需的原始结果。
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