我有一个UIView
和UIButton
(在RootViewcontroller的viewDidLoad:
方法中声明。).
UIView *geopointView = [[UIView alloc] initWithFrame:CGRectMake(0, 320, 120, 100)] ;
UIButton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 330, 120, 30)] ;
mybutton被添加到geopointView。
[geopointView addsubView:mybutton];
最后UIView被添加到RootViewController方法showTheView(在RootViewController中定义)
-(void) showTheView{
[self.view addsubView:geopointView];
}
但是当这个方法被调用时,我发现mybutton和geopointView都是可见的,但mybutton不是放置在geopointView内部。mybutton的边界现在显示为CGRectmake(10, 250, 120, 30);
谁能告诉我这里缺少什么。提前感谢您的帮助。
try this
UIbutton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 120, 30)] ;
按钮的框架应该相对于父元素的边界,但它看起来是相对于屏幕的。如果你想让你的按钮在geopointView中它需要有x, y坐标适合于geopointView的宽度(120)和高度(100)所以像这样的东西会把它放在geopointView的左上角(0,0):
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,120,30)];