first_launch_view_controller.mm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "remoting/ios/app/first_launch_view_controller.h"
  5. #import <MaterialComponents/MaterialButtons.h>
  6. #include "remoting/base/string_resources.h"
  7. #import "remoting/ios/app/remoting_theme.h"
  8. #include "ui/base/l10n/l10n_util.h"
  9. #if !defined(__has_feature) || !__has_feature(objc_arc)
  10. #error "This file requires ARC support."
  11. #endif
  12. static const float kLogoSizeMultiplier = 0.381966f;
  13. static const float kLogoYOffset = -10.f;
  14. static const float kButtonHeight = 80.f;
  15. @interface FirstLaunchViewController () {
  16. NSArray<NSLayoutConstraint*>* _compactWidthConstraints;
  17. NSArray<NSLayoutConstraint*>* _compactHeightConstraints;
  18. }
  19. @end
  20. @implementation FirstLaunchViewController
  21. @synthesize delegate = _delegate;
  22. #pragma mark - UIViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. UIImageView* imageView = [[UIImageView alloc]
  26. initWithImage:[UIImage imageNamed:@"launchscreen_app_logo"]];
  27. imageView.translatesAutoresizingMaskIntoConstraints = NO;
  28. [self.view addSubview:imageView];
  29. MDCFlatButton* signInButton = [[MDCFlatButton alloc] init];
  30. [signInButton setTitle:l10n_util::GetNSString(IDS_SIGN_IN_BUTTON)
  31. forState:UIControlStateNormal];
  32. [signInButton sizeToFit];
  33. [signInButton addTarget:self
  34. action:@selector(didTapSignIn:)
  35. forControlEvents:UIControlEventTouchUpInside];
  36. [signInButton setTitleColor:RemotingTheme.flatButtonTextColor
  37. forState:UIControlStateNormal];
  38. signInButton.translatesAutoresizingMaskIntoConstraints = NO;
  39. [self.view addSubview:signInButton];
  40. self.view.backgroundColor = RemotingTheme.firstLaunchViewBackgroundColor;
  41. [self initConstraints:imageView button:signInButton];
  42. }
  43. - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection {
  44. [self refreshTraitCollection];
  45. }
  46. #pragma mark - Private
  47. - (void)didTapSignIn:(id)button {
  48. [_delegate presentSignInFlow];
  49. }
  50. - (void)initConstraints:(UIImageView*)imageView button:(UIButton*)signInButton {
  51. // This matches the constraints in LaunchScreen.storyboard.
  52. [imageView setContentHuggingPriority:UILayoutPriorityRequired
  53. forAxis:UILayoutConstraintAxisVertical];
  54. [imageView setContentHuggingPriority:UILayoutPriorityRequired
  55. forAxis:UILayoutConstraintAxisHorizontal];
  56. [imageView
  57. setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
  58. forAxis:UILayoutConstraintAxisVertical];
  59. [imageView
  60. setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
  61. forAxis:UILayoutConstraintAxisHorizontal];
  62. _compactWidthConstraints =
  63. @[ [imageView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor
  64. multiplier:kLogoSizeMultiplier] ];
  65. _compactWidthConstraints[0].priority = UILayoutPriorityDefaultHigh;
  66. _compactHeightConstraints =
  67. @[ [imageView.heightAnchor constraintEqualToAnchor:self.view.heightAnchor
  68. multiplier:kLogoSizeMultiplier] ];
  69. _compactHeightConstraints[0].priority = UILayoutPriorityDefaultHigh;
  70. [NSLayoutConstraint activateConstraints:@[
  71. [imageView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
  72. [imageView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor
  73. constant:kLogoYOffset],
  74. [imageView.widthAnchor constraintEqualToAnchor:imageView.heightAnchor],
  75. [signInButton.leadingAnchor
  76. constraintEqualToAnchor:self.view.leadingAnchor],
  77. [signInButton.trailingAnchor
  78. constraintEqualToAnchor:self.view.trailingAnchor],
  79. [signInButton.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
  80. [signInButton.heightAnchor constraintEqualToConstant:kButtonHeight],
  81. ]];
  82. [self refreshTraitCollection];
  83. }
  84. - (void)refreshTraitCollection {
  85. if (self.traitCollection.verticalSizeClass ==
  86. UIUserInterfaceSizeClassCompact) {
  87. [NSLayoutConstraint deactivateConstraints:_compactWidthConstraints];
  88. [NSLayoutConstraint activateConstraints:_compactHeightConstraints];
  89. } else if (self.traitCollection.horizontalSizeClass ==
  90. UIUserInterfaceSizeClassCompact &&
  91. self.traitCollection.verticalSizeClass ==
  92. UIUserInterfaceSizeClassRegular) {
  93. [NSLayoutConstraint deactivateConstraints:_compactHeightConstraints];
  94. [NSLayoutConstraint activateConstraints:_compactWidthConstraints];
  95. } else {
  96. [NSLayoutConstraint deactivateConstraints:_compactWidthConstraints];
  97. [NSLayoutConstraint deactivateConstraints:_compactHeightConstraints];
  98. }
  99. }
  100. @end