我是 iOS 开发的新手,我不确定我想做的事情是否可行。 基本上我正在通过网络服务检索一些产品,我想在屏幕上显示每一个产品,每行 4 个方 block 。这些方 block 中的每一个(带有图像和产品名称)都是一个 UIView,它有一个 .h、.m 和 xib 文件。我这样创建每个 View :
CategoryItemView *catItem = [[[NSBundle mainBundle] loadNibNamed"CategoryItemView" owner:self options:nil] objectAtIndex:0];
[categoryItems addObject:catItem];
我把它定位为:
while (colCount < cols && items < [categoryItems count]) {
CGRect viewRect = CGRectMake( x * colCount + pad , (row * (x-pad)) + pad*row + pad, x - pad, x - pad );
CategoryItemView* myView = [categoryItems objectAtIndex:items];
myView.frame = viewRect;
[self.view addSubview:myView];
colCount++;
items++;
}
我想使用 xib 来布局所有元素。但是我找不到布局 xib 的方法,这样当像这样定位 UIView 时,所有元素都被缩放以适应并保持它们在 UIView 中的相对位置。这可能吗?
更新:您可以在此处查看 2 类示例,但该示例不起作用 http://home.glasscubes.com/share/s/d57sb19
谢谢
autoresizingMask 是您在这些 View 上寻找的属性。
来自文档:
autoresizingMask
An integer bit mask that determines how the receiver resizes itself when its superview’s bounds change.
@property(nonatomic) UIViewAutoresizing autoresizingMask
Discussion
When a view’s bounds change, that view automatically resizes its subviews according to each subview’s autoresizing mask. You specify the value of this mask by combining the constants described in UIViewAutoresizing using the C bitwise OR operator. Combining these constants lets you specify which dimensions of the view should grow or shrink relative to the superview. The default value of this property is UIViewAutoresizingNone, which indicates that the view should not be resized at all.
When more than one option along the same axis is set, the default behavior is to distribute the size difference proportionally among the flexible portions. The larger the flexible portion, relative to the other flexible portions, the more it is likely to grow. For example, suppose this property includes the UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleRightMargin constants but does not include the UIViewAutoresizingFlexibleLeftMargin constant, thus indicating that the width of the view’s left margin is fixed but that the view’s width and right margin may change. Thus, the view appears anchored to the left side of its superview while both the view width and the gap to the right of the view increase.
If the autoresizing behaviors do not offer the precise layout that you need for your views, you can use a custom container view and override its layoutSubviews method to position your subviews more precisely.
关于iphone - 是否有可能有一个 XIB 然后设置大小并让所有东西都缩放以适应 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7924202/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |