无法更改某个UIView中子视图的大小

时间:2016-07-27 06:43:29

标签: ios swift uiview

在视图控制器中,上下显示两个UIView。它们具有相同的宽度和相隔20个像素。

如果单击UIButton,上部视图(viewUpper)的高度应增加30像素;而较低的一个(viewLower)减少相同的量。到目前为止,我根据自己的意愿制作了视图。上层展开,下层展开。

此外,viewLower的y坐标也增加了相同的数量,因此这两个视图保持距离。

问题在于,下方视图中的子视图的更改方式与完成超级视图的方式不同。下部视图有两个向上和向下对齐的子视图。

上层子视图(viewTitle)应保持其大小。下部子视图(viewContent)必须减少30个像素。我用以下代码编写了代码:

viewContent.frame.size.height -= 30

这个没有工作。所以我用于下面写的声明,它也没有用。

enter image description here

@IBAction func changeSize(sender: UIButton) {
    //d is a Bool variable to choose whether it's turn to shrink the viewLower or not
    if d{
        d = false

        viewUpper.frame.size.height += 30
        viewLower.frame.size.height -= 30
        viewLower.frame.origin.y += 30

        //do not work
        //viewContetnt.frame.size.height -= 30

        //I tried this code to change the subviews in the same way
        for views in viewLower.subviews {
            if views.frame.size.height > 30 {
                views.frame.size.height -= 30
            }
        }
    }
    else {
        d = true

        viewUpper.frame.size.height -= 30
        viewLower.frame.size.height += 30
        viewLower.frame.origin.y -= 30

        //do not work
        //viewContetnt.frame.size.height += 30

        //I tried this code to change the subviews in the same way
        for views in viewLower.subviews {
            if views.frame.size.height > 30 {
                views.frame.size.height += 30
            }
        }
    }
}

func changeWholeViewSize(view: UIView, f: CGFloat){
    for views in view.subviews {

        if view.frame.size.height > 40 {
            views.frame.size.height -= f
        }
        if(views.subviews.count > 0) {
            changeWholeViewSize(views, f: f)
        }
    }
}

我该如何处理这个问题?我知道的两种方式对我没有帮助。

2 个答案:

答案 0 :(得分:0)

据我所知,试试这个:(使用约束)

1)将top,leading,trailing和height约束设置为 viewUpper

enter image description here

2)将前导,尾随,底部约束设置为 viewLower

3)设置两个视图之间的垂直间距,即20。

enter image description here

4)在 viewLower 中,将top,leading,trailing和height约束设置为 viewTitle 。对于其他子视图,只需将它们固定在顶部,底部和侧面(不要设置高度)。

enter image description here

enter image description here

5)现在,无论何时按下按钮,都要更改 viewUpper 的高度约束(heightConst,假设),如下所示:

b'01001011' # Is this a packed string though?
bytes(4) # This creates bytes. How do I manipulate the bits?, is this data able to send over serial?
int('01001011', 2) # Will this be treated as an integer over serial?
binascii.hexify() # This produces ASCII representation

希望这会对你有所帮助。

答案 1 :(得分:0)

要解决您的问题,您应该使用layoutSubviews(UIView的方法)。

这是我的代码:

这是ViewController.m:

TADOQuery.FieldDefs

这是LowerView.m:

#import "ViewController.h"
#import "LowerView.h"

@interface ViewController ()

@property UIView* viewUpper;
@property LowerView* viewLower;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

float viewWidth = [UIScreen mainScreen].bounds.size.width - 40;
_viewUpper = [[UIView alloc] initWithFrame:CGRectMake(20, 20, viewWidth, 200)];
_viewUpper.backgroundColor = [UIColor redColor];
[self.view addSubview:_viewUpper];

float lowerLocY = _viewUpper.frame.origin.y + _viewUpper.frame.size.height + 20;
_viewLower = [[LowerView alloc] initWithFrame:CGRectMake(20, lowerLocY, viewWidth, 300)];
_viewLower.backgroundColor = [UIColor blueColor];
[self.view addSubview:_viewLower];

UITapGestureRecognizer* tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle)];
[_viewUpper addGestureRecognizer:tapGR];
}

-(void)tapHandle{
NSLog(@"tagGR actived");
float moveDistance = 30;
_viewUpper.frame = CGRectMake(_viewUpper.frame.origin.x, _viewUpper.frame.origin.y, _viewUpper.frame.size.width, _viewUpper.frame.size.height + moveDistance);
_viewLower.frame = CGRectMake(_viewLower.frame.origin.x, _viewLower.frame.origin.y + moveDistance, _viewLower.frame.size.width, _viewLower.frame.size.height - moveDistance);
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

希望它可以帮到你。