当屏幕方向改变时,可以转换先前视图控制器上的视图吗?

2023-11-30

我有一种情况viewControllerA pushes viewControllerB到导航堆栈上。当用户旋转屏幕和方向时viewControllerB改变,我想要一个subviewA of viewControllerA来转变和重新定位自己。这可能吗?

我试过发送一个指针subviewA to viewControllerB,但是我对其位置所做的任何事情都会被忽略(当viewControllerB当前在屏幕上)。

我也尝试过重新定位subviewA in viewDidAppear of viewControllerA after viewControllerB从堆栈中弹出,但它看起来很难看,因为它在出现在屏幕上时会快速重新定位。还尝试在中操纵它viewWillAppear但什么也没发生。

我唯一能想到的就是发送viewControllerA的整个self.view to viewControllerB并将其附加为子视图viewControllerB旋转然后操纵它,然后将其移除。但这显然是一个可怕的解决方案。

有没有办法做到这一点?


可以通过以下方式进行约束:当超级视图的边界发生变化时,视图的位置和大小可以自动更改(不检查方向)。如果您以这种方式进行约束,那么无论旋转时视图是否在屏幕上,视图都会在旋转时进行正确的排列。

约束的形式为y = m*x + b其中 y 和 x 是两个视图,m 是乘数,b 是常数。它需要做一些数学计算来找出 m 和 b 的值将为您提供所需的约束。我在 NSLayoutConstraint 上创建了一个类别,允许您直接指定纵向和横向所需的值。你可以像这样使用它,

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addConstraint:[NSLayoutConstraint widthConstraintForView:self.rectangle superview:self.view portraitValue:100 landscapeValue:200]];
    [self.view addConstraint:[NSLayoutConstraint heightConstraintForView:self.rectangle superview:self.view portraitValue:150 landscapeValue:80]];
    [self.view addConstraint:[NSLayoutConstraint topConstraintForView:self.rectangle viewAttribute:NSLayoutAttributeTop superview:self.view portraitValue:200 landscapeValue:10]];
    [self.view addConstraint:[NSLayoutConstraint leftConstraintForView:self.rectangle viewAttribute:NSLayoutAttributeLeft superview:self.view portraitValue:100 landscapeValue:100]];
}

如果您在 IB 中设置了任何将被这些约束替换的约束,则可以将它们标记为将在运行时删除的占位符。该类别看起来像这样,

+(NSLayoutConstraint *)heightConstraintForView:(UIView *)subview superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
    CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
    CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeHeight relatedBy:0 toItem:superview attribute:NSLayoutAttributeHeight multiplier:multiplier constant:constant];
    NSLog(@"height coeffs: %f   %f",multiplier,constant);
    return con;
}

+(NSLayoutConstraint *)widthConstraintForView:(UIView *)subview superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
    CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.width - superview.bounds.size.height);
    CGFloat constant = pValue - (superview.bounds.size.width * multiplier);
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeWidth relatedBy:0 toItem:superview attribute:NSLayoutAttributeWidth multiplier:multiplier constant:constant];
    NSLog(@"width coeffs: %f   %f",multiplier,constant);
    return con;
}


+(NSLayoutConstraint *)leftConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
    CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.width - superview.bounds.size.height);
    CGFloat constant = pValue - (superview.bounds.size.width * multiplier);
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeRight multiplier:multiplier constant:constant];
    NSLog(@"left coeffs: %f   %f",multiplier,constant);
    return con;
}


+(NSLayoutConstraint *)rightConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
    CGFloat multiplier = (superview.bounds.size.width - pValue - superview.bounds.size.height + lValue)/(superview.bounds.size.width - superview.bounds.size.height);
    CGFloat constant = superview.bounds.size.width - pValue - (superview.bounds.size.width * multiplier);
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeRight multiplier:multiplier constant:constant];
     NSLog(@"right coeffs: %f   %f",multiplier,constant);
    return con;
}

+(NSLayoutConstraint *)topConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
    CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
    CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
     NSLog(@"top coeffs: %f   %f",multiplier,constant);
    return con;
}


+(NSLayoutConstraint *)bottomConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
    CGFloat multiplier = (superview.bounds.size.height - pValue - superview.bounds.size.width + lValue)/(superview.bounds.size.height - superview.bounds.size.width);
    CGFloat constant = superview.bounds.size.height - pValue - (superview.bounds.size.height * multiplier);
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
     NSLog(@"bottom coeffs: %f   %f",multiplier,constant);
    return con;
}

我在这里发布了这个示例项目,http://jmp.sh/SYgejs6

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当屏幕方向改变时,可以转换先前视图控制器上的视图吗? 的相关文章

随机推荐

  • 当放入结构中时,值的寿命不够长

    我正在尝试使用 Rust 中的 LLVM这个板条箱 我正在尝试创建一个代码生成器结构来为我保存上下文 模块和构建器 但是当我尝试编译时 我收到一条错误消息 c does not live long enough 我怎样才能编译它 为什么 c
  • 铸造容器 C++ [重复]

    这个问题在这里已经有答案了 可能的重复 是否可以将 Base 类型的 STL 容器转换为 Derived 类型 这应该是一个简单的问题 如果我有一个基类的容器 例如 STL 列表 是否可以将整个容器转换为子类类型 例如 A inherits
  • @Query 注释使用 like %?1%

    我想写一个这样的查询 Query select p from Product p where p name 1 or p desc like 1 但这给了我例外 org hibernate hql ast QuerySyntaxExcept
  • 这个方法如何运作?

    我经常遇到这种注册动作侦听器的方式 虽然我最近一直在使用这种方法 但我不明白这是如何以及为什么的 这是一个 submit new JButton submit submit addActionListener new ActionListe
  • 绑定到 ICollectionView 时模拟数据不显示

    如果我绑定我的ListBox到视图模型ObservableCollection或 XAML 资源CollectionViewSource 模拟数据在设计时显示 有时CollectionViewSource由于某些 XAML 更改而停止显示此
  • Eclipse 在没有互联网连接的 PC 上间歇性挂起

    我从 Eclipse 3 2 开始就遇到过这个问题 但在较新的版本中 这个问题出现得更频繁 每当我访问某些配置菜单项或 Eclipse 解析包含指向某个 Internet URL 的 xmlns 变量的 XML 文件时 Eclipse ID
  • 从 DataTable 批量插入到 SQLCE DataSource

    这是一个使用 SQL CE 作为数据源的 C WPF 应用程序 我有一个 DataTable 显示为 DataGrid 和一个 SQL CE DataSource 我使用 DataAdapter DataSet 和 DataTable 从
  • 如何将 XML 文件读取到包含 null 元素的空字符串的 Dictionary>

    我有一个 xml 文件 例如
  • 雅虎财经 python 上的某些股票和页面出现 404 错误

    我正在尝试通过此 URL 从雅虎财经抓取数据https finance yahoo com quote AAPL key statistics p AAPL 运行下面的 python 代码后 我得到以下 HTML 响应 import num
  • 反转非唯一字典

    我有一本字典 需要根据非唯一值进行反转和分组 该字典根据现有的最佳答案而失败 gt gt gt graph a car red b car blue gt gt gt inv map gt gt gt for k v in graph it
  • 在处理中获取多个音频输入

    我目前正在编写一个需要访问多个音频输入的处理草图 但处理只允许访问默认线路 我尝试直接从 Java 混合器获取线路 在处理中访问 但我仍然只获得信号从我的机器上当前设置为默认值的那一行开始 我已经开始考虑按照建议通过 OSC 从 Super
  • 通过 AJAX 进行表单处理 - 避免生成 GET 和 POST 请求

    我正在尝试通过 AJAX 发送表单数据 但我看到 GET 和 POST 请求都正在生成 这是我的表单的提交处理程序
  • std::ostream 到 QDataStream

    我已经为 MyClass 重载了 friend std ostream operator lt lt std ostream out const MyClass Obj 现在我想将 std ostream 转换为 QDataStream 而
  • 为什么ld输出的二进制文件无法执行?

    我使用的是 Ubuntu 18 04 x86 64 这是我的程序的两个源文件 main c include stdio h int sum int a int n int array 2 1 2 int main int val sum a
  • PyTables 的优点是什么? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心以获得指导 我最近开始学习PyTabl
  • 在 R 中自定义传单地图图标

    我开始学习如何使用传单地图中的搜索功能 下面是传单地图 它允许您搜索城市 即单个搜索词 library leaflet library leaflet extras library dplyr using the same reproduc
  • 如何使 Flex 仅在有用时消耗鼠标滚动和键盘事件,否则将其传递给浏览器?

    This one s been irking me for a while When I m using the mouse scroll wheel to scroll up and down in a webpage and a fla
  • 替换工作表名称中的多个无效字符[关闭]

    Closed 这个问题需要调试细节 目前不接受答案 我正在写一个相当大的宏 最后使用用户之前输入的名称保存工作簿 我为此使用了工作表的标题 因此我需要删除任何会引发文件系统错误的保留字符 lt gt 我想避免使用大量Replace 语句 那
  • 动态分配文件名到Excel连接字符串

    这是我第一次在 SQL Server 2012 中使用 SSIS 我可以成功读取 excel 文件并将其内容加载到 SQL Server 2012 中的表中 任务是一个简单的直接读取 excel 文件 然后复制到 sql server 无需
  • 当屏幕方向改变时,可以转换先前视图控制器上的视图吗?

    我有一种情况viewControllerA pushes viewControllerB到导航堆栈上 当用户旋转屏幕和方向时viewControllerB改变 我想要一个subviewA of viewControllerA来转变和重新定位