UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 70)];
containerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:containerView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(SettingPressed:) forControlEvents:UIControlEventTouchUpInside];
UIImage *image = [UIImage imageNamed:@"settings_button.png"];
[button setImage:image forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected];
button.frame = CGRectMake(0,0, 40, 40);
button.center = containerView.center;
[containerView addSubview:button];
问题是uibutton远离uiview而不是uiview的中间。我错了,请指导我。 感谢。
答案 0 :(得分:1)
首先,当您更改视图的center
属性时,frame
属性将自动更改,因此您只应设置按钮中心:
button.center = CGPointMake(containerView.frame.size.width / 2,
containerView.frame.size.height / 2);
现在,每当您更改center属性时,您需要手动指示视图使用setNeedsDisplay
重绘自身:
[button setNeedsDisplay]
最后,框架和边界之间的差异定义了相对于父视图的位置和大小,而边界描述了框架内部(或外部)的绘图区域。