简体   繁体   English

修复自定义视图在UIView中的位置

[英]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 - 我有一个自定义视图,在drawRect中添加了一些图形作为UIView的子视图。点击子视图时,我更改了基本视图的高度,这是通过-

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. 这也重新定位了我不需要的自定义视图(将其设置在基本视图的顶部),子视图应固定在UIView的底部。

Y position for the custom view is set to the height of the base view - 自定义视图的Y位置设置为基本视图的高度-

 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; 我已经设置了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. 它将使边界从左上角到屏幕右侧的点,该点比初始点低400像素。

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方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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