2014-03-13 38 views
1

尝试和搜索了很多。当超视图的框架很小时,作为子视图添加的按钮远离视图。我不希望它在帧很小时出现。作为子视图添加UIButton远离UIView

UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(20 , 100, 200, 30)]; 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; 
    [btn setTitle:@"OK" forState:UIControlStateNormal]; 
    [btn setFrame:CGRectMake(20 , 100, 30, 20)]; 
    [vw sendSubviewToBack:btn]; 
    [vw addSubview:btn]; 
    [vw setBackgroundColor:[UIColor redColor]]; 
    [self.view addSubview:vw]; 

这是我的代码。它看起来像这样

enter image description here

+0

你知道superview的界限。测试你是否放置按钮。 –

+0

检查第一个视图的UIButton层次.......它改变iOS 7 –

+0

给[btn setFrame:CGRectMake(20,0,30,20)]; – Spynet

回答

1

你做错了。在添加btn查看之前,您已拨打sendSubviewToBack:。只需重写如下。

[vw addSubview:btn];//First 
[self.view addSubview:vw];//second 
[vw sendSubviewToBack:btn];//Third 

你不希望出现,如果它是超级视角之外的谎言,使用此。 vw.clipsToBounds = YES

2

看看UIButton的框架。它的origin.y是100px。它应该是0,如果你想把它添加到vw。

相关问题