Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何渲染屏幕中央的按钮?_Ios_Objective C_Ios7_Ios8 - Fatal编程技术网

Ios 如何渲染屏幕中央的按钮?

Ios 如何渲染屏幕中央的按钮?,ios,objective-c,ios7,ios8,Ios,Objective C,Ios7,Ios8,我正在为我的应用程序使用,默认情况下,当它呈现时,它看起来像附件一样 我环顾四周,了解如何以编程方式添加约束,以便根据屏幕的大小,按钮落在屏幕的中心,因此我的代码如下所示 - (void)signInWithGooglePlus { GPPSignInButton *signInButton = [[GPPSignInButton alloc] init]; [signInButton setTranslatesAutoresizingMaskIntoConstraints:NO

我正在为我的应用程序使用,默认情况下,当它呈现时,它看起来像附件一样

我环顾四周,了解如何以编程方式添加约束,以便根据屏幕的大小,按钮落在屏幕的中心,因此我的代码如下所示

- (void)signInWithGooglePlus {
    GPPSignInButton *signInButton = [[GPPSignInButton alloc] init];
    [signInButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [signInButton setStyle:kGPPSignInButtonStyleWide];
    [signInButton setColorScheme:kGPPSignInButtonColorSchemeDark];

    NSLayoutConstraint *vConstraint = [NSLayoutConstraint constraintWithItem:signInButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    NSLayoutConstraint *hConstraint = [NSLayoutConstraint constraintWithItem:signInButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:vConstraint];
    [self.view addConstraint:hConstraint];

    [self.view addSubview:signInButton];

    GPPSignIn *signIn = [GPPSignIn sharedInstance];
    signIn.shouldFetchGooglePlusUser = YES;
    signIn.shouldFetchGoogleUserEmail = YES;

    signIn.clientID = kClientId;
    signIn.scopes = @[@"profile"];
    signIn.delegate = self;
    [signIn trySilentAuthentication];
}
- (void)signInWithGooglePlus {
    GPPSignInButton *signInButton = [[GPPSignInButton alloc] init];
    [signInButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [signInButton setStyle:kGPPSignInButtonStyleWide];
    [signInButton setColorScheme:kGPPSignInButtonColorSchemeDark];

    CGRect frame = signInButton.frame;
    frame.origin.x = (self.view.frame.size.width - signInButton.frame.size.width) / 2;
    frame.origin.y = (self.view.frame.size.height - signInButton.frame.size.height) / 2;
    signInButton.frame = frame;

    [self.view addSubview:signInButton];
    GPPSignIn *signIn = [GPPSignIn sharedInstance];
    signIn.shouldFetchGooglePlusUser = YES;
    signIn.shouldFetchGoogleUserEmail = YES;

    signIn.clientID = kClientId;
    signIn.scopes = @[@"profile"];
    signIn.delegate = self;
    [signIn trySilentAuthentication];
}
然后被叫来

- (void)viewDidLoad {
    [super viewDidLoad];
    [self signInWithGooglePlus];
}
当我运行它时,它会失败

2015-03-14 16:50:11.265 myapp-ios[24332:1661610] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7ff4c0c1b490 GPPSignInButton.centerY == UIView:0x7ff4c0f54930.centerY   (Names: GPPSignInButton:0x7ff4c0f55da0 )>
    When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-03-14 16:50:11.267 myapp-ios[24332:1661610] View hierarchy unprepared for constraint.
    Constraint: <NSLayoutConstraint:0x7ff4c0c1b490 GPPSignInButton.centerY == UIView:0x7ff4c0f54930.centerY   (Names: GPPSignInButton:0x7ff4c0f55da0 )>
    Container hierarchy: 
<UIView: 0x7ff4c0f54930; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff4c0f52d20>>
    View not found in container hierarchy: <GPPSignInButton: 0x7ff4c0f55da0; baseClass = UIButton; frame = (0 0; 226 48); opaque = NO; layer = <CALayer: 0x7ff4c0f542b0>>
    That view's superview: NO SUPERVIEW
2015-03-14 16:50:11.279 myapp-ios[24332:1661610] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view.  Does the constraint reference something from outside the subtree of the view?  That's illegal. constraint:<NSLayoutConstraint:0x7ff4c0c1b490 GPPSignInButton.centerY == UIView:0x7ff4c0f54930.centerY   (Names: GPPSignInButton:0x7ff4c0f55da0 )> view:<UIView: 0x7ff4c0f54930; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff4c0f52d20>>'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010bb93a75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010b828bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010bb939ad +[NSException raise:format:] + 205
    3   Foundation                          0x00000001094d5689 -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 187
    4   UIKit                               0x000000010a21bed5 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474
    5   Foundation                          0x00000001094e32fe -[NSISEngine withBehaviors:performModifications:] + 155
    6   UIKit                               0x000000010a21bcdb __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
    7   UIKit                               0x000000010a21baee -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
    8   UIKit                               0x000000010a21b761 -[UIView(AdditionalLayoutSupport) _initializeHostedLayoutEngine] + 404
    9   UIKit                               0x000000010a21c281 -[UIView(AdditionalLayoutSupport) _layoutEngineCreateIfNecessary] + 53
    10  UIKit                               0x000000010a210bfa -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 156
    11  UIKit                               0x000000010a210f94 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 30
    12  UIKit                               0x000000010a2110bc -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 243
    13  myapp-ios                        0x0000000108f0fd5d -[GooglePlusLoginViewController signInWithGooglePlus] + 557
    14  myapp-ios                        0x0000000108f0fa7b -[GooglePlusLoginViewController handleAuthentication] + 43
    15  myapp-ios                        0x0000000108f0fa44 -[GooglePlusLoginViewController viewDidLoad] + 228
    16  UIKit                               0x0000000109cba580 -[UIViewController loadViewIfRequired] + 738
    17  UIKit                               0x0000000109cba77e -[UIViewController view] + 27
    18  UIKit                               0x0000000109bd9509 -[UIWindow addRootViewControllerViewIfPossible] + 58
    19  UIKit                               0x0000000109bd98a1 -[UIWindow _setHidden:forced:] + 247
    20  UIKit                               0x0000000109be5f8c -[UIWindow makeKeyAndVisible] + 42
    21  myapp-ios                        0x0000000108f0f483 -[AppDelegate application:didFinishLaunchingWithOptions:] + 627
    22  UIKit                               0x0000000109b8f458 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 248
    23  UIKit                               0x0000000109b90002 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2540
    24  UIKit                               0x0000000109b92e3e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
    25  UIKit                               0x0000000109b91d35 -[UIApplication workspaceDidEndTransaction:] + 179
    26  FrontBoardServices                  0x000000010f7a7243 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    27  CoreFoundation                      0x000000010bac8c7c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    28  CoreFoundation                      0x000000010babe9c5 __CFRunLoopDoBlocks + 341
    29  CoreFoundation                      0x000000010babe785 __CFRunLoopRun + 2389
    30  CoreFoundation                      0x000000010babdbc6 CFRunLoopRunSpecific + 470
    31  UIKit                               0x0000000109b917a2 -[UIApplication _run] + 413
    32  UIKit                               0x0000000109b94580 UIApplicationMain + 1282
    33  myapp-ios                        0x0000000108f0f1e3 main + 115
    34  libdyld.dylib                       0x000000010d104145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
2015-03-14 16:50:11.265 myapp ios[24332:1661610]视图层次结构未为约束做好准备:
添加到视图时,约束项必须是该视图(或视图本身)的后代。如果在装配视图层次之前需要解决约束,则此操作将崩溃。中断-[UIView\u viewHierarchyUnpreparedForConstraint:]以进行调试。
2015-03-14 16:50:11.267 myapp ios[24332:1661610]视图层次结构未为约束做好准备。
约束条件:
容器层次结构:
在容器层次结构中找不到视图:
该视图是superview:无superview
2015-03-14 16:50:11.279 myapp ios[24332:1661610]***由于未捕获的异常“NSGenericeException”而终止应用程序,原因:“无法在视图上安装约束”。约束是否引用了视图子树之外的内容?那是违法的。约束:视图:'
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x000000010bb93a75例外预处理+165
1 libobjc.A.dylib 0x000000010b828bb7 objc_异常_抛出+45
2 CoreFoundation 0x000000010bb939ad+[N异常提升:格式:][205
3基金会0x000 000 01094D5689- [ NSLAOUTOND约束-附加引擎:整合式调整:互斥:+] 187
4 UIKit 0x000000010a21bed5 \uuU 57-[UIView(附加布局支持)\u switchToLayoutEngine:]\u block\u invoke\u 2+474
5基金会0x000 000 01094E32 FE-[ NSISEngice附带行为:PrimultExp修改:] + 155
6 UIKit 0x000000010a21bcdb\uuuU 57-[UIView(附加布局支持)\u switchToLayoutEngine:]\u block\u invoke+452
7 UIKit 0x000000010a21baee-[UIView(附加布局支持)\切换到布局引擎:+197
8 UIKit 0x000000010a21b761-[UIView(额外布局支持)\初始化后布局引擎]+404
9 UIKit 0x000000010a21c281-[UIView(附加布局支持)\u布局工程创建(如有必要)]+53
10 UIKit 0x000000010a210bfa-[UIView(UIConstraintBasedLayout)\u layoutEngine\u didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:+156
11 UIKit 0x000000010a210f94-[UIView(UIConstraintBasedLayout)\尝试在不更新约束阵列的情况下添加约束:舍入调整:相互排斥约束:+30
12 UIKit 0x000000010a2110bc-[UIView(UIConstraintBasedLayout)\尝试添加约束:舍入调整:mutuallyExclusiveConstraints:+243
13 myapp ios 0x0000000108f0fd5d-[GoogleplusGinViewController signInWithGooglePlus]+557
14 myapp ios 0x0000000108f0fa7b-[GooglePlusLoginViewController handleAuthentication]+43
15 myapp ios 0x0000000108f0fa44-[GooglePlusLoginViewController viewDidLoad]+228
16 UIKit 0x0000000109cba580-[UIViewController loadViewIfRequired]+738
17 UIKit 0x0000000109cba77e-[UIViewController视图]+27
18 UIKit 0x0000000109bd9509-[UIWindow AddRootViewControllerServiceWifPossible]+58
19 UIKit 0x0000000109bd98a1-[UIWindow\u setHidden:强制:][247
20 UIKit 0x0000000109be5f8c-[UIWindow makeKeyAndVisible]+42
21 myapp ios 0x0000000108f0f483-[AppDelegate应用程序:didFinishLaunchingWithOptions:+627
22 UIKit 0x0000000109b8f458-[UIApplication\u处理的远程回调,带选项:isSuspended:restoreState:+248
23 UIKit 0x0000000109b90002-[UIApplication\u callInitializationDelegatesForMainScene:transitionContext:+2540
24 UIKit 0x0000000109b92e3e-[UIApplication\u在主场景中运行:transitionContext:completion:+1349
25 UIKit 0x0000000109b91d35-[UIApplication WorkspaceDiEndTransaction:][179
26 FrontBoardServices 0x000000010f7a7243-[FBSSerialQueue performAsync:][U block_invoke+16
27 CoreFoundation 0x000000010bac8c7c\uuuu CFRUNLOOP\u正在调用\u OUT\u到\u块\uuuu+12
28 CoreFoundation 0x0000000109C5\uU CFRunLoopDoBlocks+341
29 CoreFoundation 0x000000010785\uuu CFRunLoopRun+2389
30 CoreFoundation 0x000000010babdbc6 CFRunLoopRunSpecific+470
31 UIKit 0x0000000109b917a2-[UIU应用程序运行]+413
32 UIKit 0x0000000109b94580 UIApplicationMain+1282
33 myapp ios 0x0000000108f0f1e3主节点+115
34 libdyld.dylib 0x000000010d104145开始+1
)
libc++abi.dylib:以NSException类型的未捕获异常终止
问题
如何将我的
GPSSignInButton
置于屏幕中央


编辑:

我发布此答案时不知道自动布局,请忽略它并使用约束

旧答案: 我从未使用过NSLAYOUTSCONSTRAINTS,但一种简单的方法是如下设置按钮的帧原点:


编辑:

我不知道自动布局
[self.view addSubview:signInButton];
NSLayoutConstraint *vConstraint = [NSLayoutConstraint constraintWithItem:signInButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
NSLayoutConstraint *hConstraint = [NSLayoutConstraint constraintWithItem:signInButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
[self.view addConstraint:vConstraint];
[self.view addConstraint:hConstraint];
CGRect frame = signInButton.frame;
frame.origin.x = (self.view.frame.size.width - signInButton.frame.size.width) / 2;
frame.origin.y = (self.view.frame.size.height - signInButton.frame.size.height) / 2;
signInButton.frame = frame;
- (void)signInWithGooglePlus {
    GPPSignInButton *signInButton = [[GPPSignInButton alloc] init];
    [signInButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [signInButton setStyle:kGPPSignInButtonStyleWide];
    [signInButton setColorScheme:kGPPSignInButtonColorSchemeDark];

    CGRect frame = signInButton.frame;
    frame.origin.x = (self.view.frame.size.width - signInButton.frame.size.width) / 2;
    frame.origin.y = (self.view.frame.size.height - signInButton.frame.size.height) / 2;
    signInButton.frame = frame;

    [self.view addSubview:signInButton];
    GPPSignIn *signIn = [GPPSignIn sharedInstance];
    signIn.shouldFetchGooglePlusUser = YES;
    signIn.shouldFetchGoogleUserEmail = YES;

    signIn.clientID = kClientId;
    signIn.scopes = @[@"profile"];
    signIn.delegate = self;
    [signIn trySilentAuthentication];
}