简体   繁体   中英

Fix position of custom view in UIView

I have a custom view with some drawing in drawRect added as subview in a UIView.On tapping on the subview I change the height of the base view.This is done by -

CGRect rect = self.bounds;
rect.size.height = 400.0;
self.bounds = rect;

This also repositions the custom view (sets it to the top of the base view) which I don't want , the subview should remain fixed at the bottom of the UIView.

Y position for the custom view is set to the height of the base view -

 y = self.bounds.size.height - 40.0;

This works when the custom control is added for the first time, but does not work when the base view changes height. I have set self.autoresizesSubviews = NO; for the base view.

If I remove the custom view from the base view and add it again after the height changes nothing is shown , just a blank base view.

Any ideas to fix the position of the custom view to the bottom of the base view.

Try to use

self.bounds = CGRectMake(0, 0, 320, 400);

It will make the bounds from top left corner to point on the right side of the screen that is 400 pixels below the initial point.

You can also try using method

-(void)layoutSubviews

I don't remember the correct syntax, but I'm pretty sure it was like that.

hope it helps

在该视图上触发setNeedsDisplay方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM