如何使用故事板设置 UIView init 的大小? [英] How can I set the size of a UIView init with storyboard?

查看:51
本文介绍了如何使用故事板设置 UIView init 的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是 iOS 6.0.

I am currently using iOS 6.0.

我有一个自定义的 UIView 需要有一定的大小.如果我以编程方式初始化视图并添加它就可以了.但是,我在storyboard中找不到可以设置视图大小的地方.

I have a custom UIView that needs to have a certain size. If I programmatically init the view and add it it's fine. However, I can't find a place where I can set the size of the view in the storyboard.

在情节提要中设置其大小不起作用,因为情节提要认为它是空的并将其大小设置为零.在 viewDidLoadviewDidAppear 中设置其大小不起作用,因为稍后该大小将被 _applyISEngineLayoutValue 覆盖.

Setting its size in the storyboard doesn't work because the storyboard thinks it's empty and set it's size to zero. Setting its size in viewDidLoad or viewDidAppear doesn't work because later on the size will be overwritten by _applyISEngineLayoutValue.

推荐答案

我面临同样的问题,我认为简短的回答是:在使用故事板时,您不能依赖视图的 init 方法中的框架或边界.

I am facing the same problem, and I think the short answer is: you can't rely on the frame or bounds in the view's init method when using storyboards.

当您的视图被 segue 初始化时,它将调用 initWithCoder: 方法而不是其他 init 方法,因为它正在从 .xib 文件反序列化您的视图对象.在故事板之前,框架和边界将在调用 initWithCoder: 时设置,但故事板似乎不再如此——iOS 代码稍后设置这些值.

When your view is initialized by the segue, it will call the initWithCoder: method rather than other init methods, since it is deserializing your view object from the .xib file. Before storyboards, the frame and bounds would be set by the time the initWithCoder: was called, but that appears to no longer be the case with storyboards -- the iOS code sets those values later.

我根据情况使用了几种解决方法:

I've used a couple workarounds, depending on the situation:

  1. 如果我事先知道视图的尺寸(例如只支持一种尺寸的特殊视图),我会在 initWithCoder: 方法中设置我自己的框架.

  1. If I know the size of the view in advance (for example a specialty view that only supports one size) I set my own frame in the initWithCoder: method.

如果我不知道视图的大小,我会将特定于大小的东西的初始化推迟到我的视图的 layoutSubviews 方法被调用.

If I don't know the size of the view, I defer initialization of size-specific things until my view's layoutSubviews method is called.

如果更方便的话,我有时只是在视图的 ViewController 中明确地执行特定于尺寸的初始化.(不漂亮,但有时又快又容易.我不骄傲.)

If it's more convenient, I sometimes just explicitly do the size-specific initialization in my view's ViewController. (Not pretty, but sometimes quick-and-easy. I'm not proud.)

这篇关于如何使用故事板设置 UIView init 的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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