使用XCTest测试自动布局 [英] Testing autolayout with XCTest
问题描述
我正在尝试确定是否有一种方法可以在使用自动布局时在单元测试中测试iOS视图的布局.现在,我尝试简单地初始化视图控制器,然后检查视图的框架.但是,每个视图上的帧仍为原点=(x = 0,y = 0)大小=(宽度= 0,高度= 0).
I'm trying to figure out if there is a way to test the layout of an iOS view in unit tests when using autolayout. Right now I try to simply initialize the view controller, and then check the frames of the views. However, the frame on each view remains origin=(x=0, y=0) size=(width=0, height=0).
- (void)setUp {
[super setUp];
_viewController = [[AddPlayerViewController alloc] init];
_viewController.view.frame = CGRectMake(0, 0, 320, 460);
[_viewController view];
}
- (void)testViewsAreInsideWindow {
[self checkIfViewIsInsideWindow:_viewController.txtfNewPlayer];
[self checkIfViewIsInsideWindow:_viewController.btnNewPlayer];
[self checkIfViewIsInsideWindow:_viewController.tblPlayers];
}
- (void)checkIfViewIsInsideWindow:(UIView *)view {
CGRect windowFrame = _viewController.view.frame;
CGRect viewFrame = view.frame;
XCTAssertTrue(CGRectContainsRect(windowFrame, viewFrame));
}
我尝试添加
[_viewController.view needsUpdateConstraints];
或
[_viewController.view updateConstraints];
或
[_viewController updateViewConstraints];
或
[_viewController viewWillAppear:YES];
但他们都没有帮助.
使用XCTest时是否可以自动布局运行?
Is it at all possible to get autolayout to run when using XCTest?
推荐答案
您尝试过setNeedsLayout
,然后是layoutIfNeeded
吗?
Have you tried setNeedsLayout
followed by layoutIfNeeded
?
You can definitely get layout to run in tests, I do it here, but that doesn't have a view controller, just views.
这篇关于使用XCTest测试自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!