0
votes

I can't seem to figure out what I'm doing wrong here. I want to stick a UIView in the bottom right corner of the screen, but the alignment is off. In my view did load I have...$

int size = 50;
CGRect frame = CGRectMake(self.view.frame.size.width - (size + 10), self.view.frame.size.height - (size + 10), size, size);
UIView *colorView = [[UIView alloc]initWithFrame:frame];
colorView.layer.cornerRadius = size / 2;
colorView.layer.masksToBounds = YES;
colorView.backgroundColor = ColorDarkGray;
[self.view addSubview:colorView];

Now to me, this should be putting the view in the bottom right corner with a bit of padding. But it is completely off the screen. I have auto layout shut off, and I'm just stuck. Could someone please help me figure this out.

1
Comment out masktoboundsJason Renaldo
Did you check what was the value of self.view.frame at this point? Depending where you implemented this, it may be that self.view has not yet been lay out.Guillaume Algis
probably you are not using iPhone portrait mode.did you try to do it in viewWillAppear ?saurabh
Autolayout / autoresizing? What rules are applied?Wain
viewWillAppear did it man. Thank you. Figured ViewDidLoad would be the right place but clearly not. Thanks.Ian

1 Answers

0
votes

I know this is quite old by now - but I stumbled over the same issue and found a solution.

Basically, if you read out the self.view.frame values in viewDidLoad or something similar, the layouting seems not yet to be done, so the frame is off.

Try overriding viewWillLayoutSubviews and set the frame there again after [super viewWillLayoutSubviews]; - and it will work!

Please note that adding the view in viewWillAppear may not be called only once, so the view could be added multiple times!