在可可触摸中以编程方式设置视图位置 [英] Set view position to top right programmatically in cocoa touch

查看:120
本文介绍了在可可触摸中以编程方式设置视图位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确保一个视图A(大小:200x200)始终与第二个视图B(全屏尺寸)内的右上角对齐。我想确保视图A保持在该位置,无论设备方向如何。真理是我没有问题,当使用界面构建器来定位视图,但我需要以编程方式构造。我想我应该使用一些自动调整设置,你能告诉我应该把视图对齐它的超视图的右上角。

解决方案

  UIView parentView //全屏视图
UIView视图// 200x200视图

[parentView addSubview:view];
CGRect frame = view.frame;

//右上方对齐
CGFloat xPosition = CGRectGetWidth(parentView.frame) - CGRectGetWidth(frame);
frame.origin = CGPointMake(ceil(xPosition),0.0);
view.frame = frame;

//自动调整大小,因此它保持在右上角(灵活的左边和灵活的底边距)
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin

将视图定位在右上角,并设置自动调整掩码,使其保持在该位置。 / p>

I need to make sure that one view A (size: 200x200) is always aligned to top right corner inside second view B (full screen size). I want to make sure that view A stays in that place regardless of device orientation. Truth is I have no problem with this when using interface builder to position the views but I need to construct this programmatically. I suppose I should use some autoresizing settings, could you tell me which one is supposed to align the view to top right corner of its superview?

解决方案

UIView parentView  //your full screen view
UIView view //the 200x200 view

[parentView addSubview:view];
CGRect frame = view.frame;

//align on top right
CGFloat xPosition = CGRectGetWidth(parentView.frame) - CGRectGetWidth(frame);
frame.origin = CGPointMake(ceil(xPosition), 0.0);
view.frame = frame;

//autoresizing so it stays at top right (flexible left and flexible bottom margin)
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin

That positions the view on top right and set's the autoresizing mask so it stays at that position.

这篇关于在可可触摸中以编程方式设置视图位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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