• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iphone - 是否有可能有一个 XIB 然后设置大小并让所有东西都缩放以适应 UIView?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:24:51 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我是 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

谢谢



Best Answer-推荐答案


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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap