为什么在ViewDidLayoutSubviews中没有更新UIView的框架? [英] Why a frame of a UIView is not updating in ViewDidLayoutSubviews?

查看:127
本文介绍了为什么在ViewDidLayoutSubviews中没有更新UIView的框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新 UIView 的框架,其中包含按钮和标签。我想在 viewDidLayoutSubviews 中更新它(我也试过 viewDidLoad viewWillAppear viewDidAppear ..)。我想更改视图的y位置(origin.y)。
NSLogs说我的原始y位置是334,更改后,它是100.但是,在我看来,位置不会改变。我已经检查过故事板中的视图已连接。我做错了什么?

I am trying to update the frame of a UIView which contains buttons and labels inside. I am trying to update it in viewDidLayoutSubviews (and I also tried in viewDidLoad, viewWillAppear, viewDidAppear..). I want to change the y position (origin.y) of the view. The NSLogs says my original y position is 334, and after changing, it is 100. However, the position does not change in my view. I have already checked that the view is connected in the storyboard. What am I doing wrong?

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGRect theFrame  = [self.bottomView frame];
    NSLog(@"Y position bottomview: %f", self.bottomView.frame.origin.y);

    if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
    {
       // NSLog(@"iphone5");
    }
    else{
       // NSLog(@"iphone4");
        theFrame .origin.y =  100;
    }

    self.bottomView.frame = theFrame;
    NSLog(@"Y position bottomview after changing it: %f", self.bottomView.frame.origin.y);
    [self.view layoutIfNeeded];
}


推荐答案

问题与Autolayout有关。但是我无法关闭它,因为我在我的项目中使用autolayout。我解决了在视图中定义适当的约束。然后无需检查它是iPhone 4英寸还是3.5英寸并更改框架的位置,因为它会自动适应每种尺寸。

The problem was related with Autolayout. However I couldn't turn it off since I am using autolayout in my project. I solved defining appropriate constraints in the view. Then there is no need to check if it is iPhone 4inch or 3.5inch and change the position of the frame since it automatically adapts to each size.

这篇关于为什么在ViewDidLayoutSubviews中没有更新UIView的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆