Rajesh
Rajesh

Reputation: 10424

UIButton added as subview is away from UIView

Tried and searched a lot. Button added as subview is away from view when the frame of superview is small. I dont want it to be appear when frame is small.

 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];

this is my code. It look like this

enter image description here

Upvotes: 1

Views: 94

Answers (2)

Krivoblotsky
Krivoblotsky

Reputation: 1502

Look on the frame of the UIButton. Its origin.y is 100px. It should be 0, if you want to add it to the vw.

Upvotes: 2

Mani
Mani

Reputation: 17585

You did wrong. Before adding btn to view, you've called sendSubviewToBack:. Just rewrite as below.

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

You don't want to appear if it's lie outside superview, use this. vw.clipsToBounds = YES

Upvotes: 1

Related Questions