有没有办法在代码中获得UIView的框架(在故事板中创建)

时间:2017-09-13 07:23:12

标签: ios swift uiview

我在故事板中创建了一个UIView,并希望为此视图添加一些内部阴影。我还为此视图创建了IBoutlet。有没有办法获得视图的框架?

@IBOutlet weak var display: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    print(display.frame.height)
}

错误

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Calculator.ViewController 0x7fba77608cd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key screen.'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010d7a2b0b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x0000000109cd5141 objc_exception_throw + 48
2   CoreFoundation                      0x000000010d7a2a59 -[NSException raise] + 9
3   Foundation                          0x00000001097eb00b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
4   UIKit                               0x000000010b09e994 -[UIViewController setValue:forKey:] + 87
5   UIKit                               0x000000010b30ba09 -[UIRuntimeOutletConnection connect] + 109
6   CoreFoundation                      0x000000010d748e8d -[NSArray makeObjectsPerformSelector:] + 269
7   UIKit                               0x000000010b30a3bf -[UINib instantiateWithOwner:options:] + 1856
8   UIKit                               0x000000010b0a4fc3 -[UIViewController _loadViewFromNibNamed:bundle:] + 381
9   UIKit                               0x000000010b0a58d9 -[UIViewController loadView] + 177
10  UIKit                               0x000000010b0a5c0a -[UIViewController loadViewIfRequired] + 195
11  UIKit                               0x000000010b0a645a -[UIViewController view] + 27
12  UIKit                               0x000000010af6e98a -[UIWindow addRootViewControllerViewIfPossible] + 65
13  UIKit                               0x000000010af6f070 -[UIWindow _setHidden:forced:] + 294
14  UIKit                               0x000000010af81ebe -[UIWindow makeKeyAndVisible] + 42
15  UIKit                               0x000000010aefb37f -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4346
16  UIKit                               0x000000010af015e4 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709
17  UIKit                               0x000000010aefe7f3 -[UIApplication workspaceDidEndTransaction:] + 182
18  FrontBoardServices                  0x0000000111b715f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
19  FrontBoardServices                  0x0000000111b7146d -[FBSSerialQueue _performNext] + 186
20  FrontBoardServices                  0x0000000111b717f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
21  CoreFoundation                      0x000000010d748c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
22  CoreFoundation                      0x000000010d72e0cf __CFRunLoopDoSources0 + 527
23  CoreFoundation                      0x000000010d72d5ff __CFRunLoopRun + 911
24  CoreFoundation                      0x000000010d72d016 CFRunLoopRunSpecific + 406
25  UIKit                               0x000000010aefd08f -[UIApplication _run] + 468
26  UIKit                               0x000000010af03134 UIApplicationMain + 159
27  Calculator                          0x00000001096fa327 main + 55
28  libdyld.dylib                       0x000000010e74265d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

帮助赞赏

2 个答案:

答案 0 :(得分:4)

您说的是一个视图但是您已经绘制了IBOutlet UILabel。 同样yourView.frame肯定会给你框架。 同时尝试删除以前的插座并重新绘制。

答案 1 :(得分:1)

您已为UILabel添加了一个插座,但在此处添加相同内容以获取框架display.frame。如果您需要,请在视图中添加一个插座,然后只添加view.frame

无论是在故事板中创建的视图还是以编程方式创建的视图都无关紧要,您始终可以通过以下方式获取框架:

view.frame

但如果您特别想要在故事板中创建的框架,则需要创建IBOutlet