2017-09-13 423 views
0

中创建一个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 

帮助表示赞赏

+0

display.superview?.frame – Pushpendra

+0

也还是让我知道什么是您的打印(display.frame)打印.. – Ishika

回答

4

你的意思是它的一个观点,但你画的UILabelIBOutlet。 也yourView.frame一定会给你的框架。 也尝试删除以前的插座,并重新绘制它。

+0

它刚刚打破..当我打电话打印(mylabel.frame) – jin

+0

你检查你的插座正确绘制? – Ishika

+0

也让我知道你在哪里写这个打印声明? – Ishika

1

您已经添加了一个UILabel的插座,但同样在那里获得框架display.frame。如果你想要,然后只是view.frame,添加一个插座到你的视图。

如果it'sa在故事板视图中创建或编程,您随时都可以得到帧它这么想的关系:

view.frame 

但是,如果你想专门你已经在你的故事板创建的框架那么你需要创建一个IBOutlet

+0

是我创建了一个IBOutlet中并调用印刷(myView.frame)都碎了 – jin

+0

做的时候你的出口左边有一个圆形的黑点? –

+0

是我控制从故事板拖..更新错误消息 – jin

相关问题