UIView+RAConstraint.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. //
  2. // UIView+RAConstraint.m
  3. // Constraint
  4. //
  5. // Created by Jack on 2018/12/10.
  6. // Copyright © 2018年 Jack Template. All rights reserved.
  7. //
  8. #import "UIView+RAConstraint.h"
  9. #import <objc/runtime.h>
  10. #pragma mark - Constraint
  11. @interface RAConstraint ()
  12. @property (nonatomic,weak) id appliedView;
  13. @property (nonatomic,weak) NSLayoutConstraint *constraint;
  14. @property (nonatomic,assign) UILayoutPriority priority;
  15. @property (nonatomic,assign) BOOL validate;
  16. @property (nonatomic,copy) NSString *identifier;
  17. @property (nonatomic,assign) NSLayoutAttribute attribute;
  18. @property (nonatomic,assign) NSLayoutRelation relation;
  19. @property (nonatomic,weak) UIView *toView;
  20. @property (nonatomic,assign) NSLayoutAttribute toAttribute;
  21. @property (nonatomic,assign) CGFloat constant;
  22. @property (nonatomic,copy) RAConstraint *(^ra_p_offset)(CGFloat offset);
  23. @property (nonatomic,copy) RAConstraint *(^ra_p_lessThanOrEqualTo)(RAConstraint *constraint);
  24. @property (nonatomic,copy) RAConstraint *(^ra_p_equalTo)(RAConstraint *constraint);
  25. @property (nonatomic,copy) RAConstraint *(^ra_p_greaterThanOrEqualTo)(RAConstraint *constraint);
  26. @property (nonatomic,copy) RAConstraint *(^ra_p_priority)(UILayoutPriority prioruty);
  27. @property (nonatomic,strong) NSMutableDictionary<NSString *,RAConstraint *> *appendConstraints;
  28. @property (nonatomic,copy) RAConstraint *(^ra_p_append)(NSString *identifier);
  29. @property (nonatomic,copy) RAConstraint *(^ra_p_size_greaterThanOrEqualTo)(CGFloat size);
  30. @property (nonatomic,copy) RAConstraint *(^ra_p_size_lessThanOrEqualTo)(CGFloat size);
  31. @property (nonatomic,copy) void (^ra_p_modify)(CGFloat constant);
  32. @end
  33. @implementation RAConstraint
  34. - (instancetype)init {
  35. if (self = [super init]) {
  36. _attribute = NSLayoutAttributeNotAnAttribute;
  37. _relation = NSLayoutRelationEqual;
  38. _toAttribute = NSLayoutAttributeNotAnAttribute;
  39. _priority = UILayoutPriorityDefaultHigh;
  40. __weak typeof(self) weakSelf = self;
  41. self.ra_p_offset = [^RAConstraint *(CGFloat offset) {
  42. weakSelf.validate = YES;
  43. weakSelf.constant = offset;
  44. return weakSelf;
  45. } copy];
  46. self.ra_p_lessThanOrEqualTo = [^RAConstraint *(RAConstraint *constraint) {
  47. weakSelf.validate = YES;
  48. weakSelf.toView = constraint.appliedView;
  49. weakSelf.relation = NSLayoutRelationLessThanOrEqual;
  50. weakSelf.toAttribute = constraint.attribute;
  51. return weakSelf;
  52. } copy];
  53. self.ra_p_equalTo = [^RAConstraint *(RAConstraint *constraint) {
  54. weakSelf.validate = YES;
  55. weakSelf.toView = constraint.appliedView;
  56. weakSelf.relation = NSLayoutRelationEqual;
  57. weakSelf.toAttribute = constraint.attribute;
  58. return weakSelf;
  59. } copy];
  60. self.ra_p_greaterThanOrEqualTo = [^RAConstraint *(RAConstraint *constraint) {
  61. weakSelf.validate = YES;
  62. weakSelf.toView = constraint.appliedView;
  63. weakSelf.relation = NSLayoutRelationGreaterThanOrEqual;
  64. weakSelf.toAttribute = constraint.attribute;
  65. return weakSelf;
  66. } copy];
  67. self.ra_p_priority = [^RAConstraint *(UILayoutPriority priority) {
  68. weakSelf.priority = priority;
  69. return weakSelf;
  70. } copy];
  71. self.ra_p_append = [^RAConstraint *(NSString *identifier) {
  72. if (!identifier) {
  73. return nil;
  74. }
  75. if (weakSelf.appendConstraints[identifier]) {
  76. return weakSelf.appendConstraints[identifier];
  77. }
  78. RAConstraint *constraint = [RAConstraint new];
  79. constraint.appliedView = weakSelf.appliedView;
  80. constraint.attribute = weakSelf.attribute;
  81. weakSelf.appendConstraints[identifier] = constraint;
  82. return constraint;
  83. } copy];
  84. self.ra_p_size_lessThanOrEqualTo = [^RAConstraint *(CGFloat size) {
  85. if (weakSelf.attribute != NSLayoutAttributeWidth && weakSelf.attribute != NSLayoutAttributeHeight) {
  86. return weakSelf;
  87. }
  88. weakSelf.validate = YES;
  89. weakSelf.toView = nil;
  90. weakSelf.relation = NSLayoutRelationLessThanOrEqual;
  91. weakSelf.toAttribute = NSLayoutAttributeNotAnAttribute;
  92. weakSelf.ra_offset(size);
  93. return weakSelf;
  94. } copy];
  95. self.ra_p_size_greaterThanOrEqualTo = [^RAConstraint *(CGFloat size) {
  96. if (weakSelf.attribute != NSLayoutAttributeWidth && weakSelf.attribute != NSLayoutAttributeHeight) {
  97. return weakSelf;
  98. }
  99. weakSelf.validate = YES;
  100. weakSelf.toView = nil;
  101. weakSelf.relation = NSLayoutRelationGreaterThanOrEqual;
  102. weakSelf.toAttribute = NSLayoutAttributeNotAnAttribute;
  103. weakSelf.ra_offset(size);
  104. return weakSelf;
  105. } copy];
  106. self.ra_p_modify = [^(CGFloat constant) {
  107. weakSelf.constant = constant;
  108. if (weakSelf.constraint) {
  109. weakSelf.constraint.constant = constant;
  110. if ([weakSelf appliedViewIsView]) {
  111. UIView *appliedView = ((UIView *)weakSelf.appliedView);
  112. [appliedView.superview layoutIfNeeded];
  113. }
  114. }
  115. } copy];
  116. }
  117. return self;
  118. }
  119. - (RAConstraint *(^)(CGFloat))ra_offset {
  120. return self.ra_p_offset;
  121. }
  122. - (RAConstraint *(^)(RAConstraint *))ra_lessThanOrEqualTo {
  123. return self.ra_p_lessThanOrEqualTo;
  124. }
  125. - (RAConstraint *(^)(RAConstraint *))ra_equalTo {
  126. return self.ra_p_equalTo;
  127. }
  128. - (RAConstraint *(^)(RAConstraint *))ra_greaterThanOrEqualTo {
  129. return self.ra_p_greaterThanOrEqualTo;
  130. }
  131. - (RAConstraint *(^)(UILayoutPriority))ra_priority {
  132. return self.ra_p_priority;
  133. }
  134. - (RAConstraint *(^)(NSString *))ra_append {
  135. return self.ra_p_append;
  136. }
  137. - (RAConstraint *(^)(CGFloat))ra_size_lessThanOrEqualTo {
  138. return self.ra_p_size_lessThanOrEqualTo;
  139. }
  140. - (RAConstraint *(^)(CGFloat))ra_size_greaterThanOrEqualTo {
  141. return self.ra_p_size_greaterThanOrEqualTo;
  142. }
  143. - (void (^)(CGFloat))ra_modify {
  144. return self.ra_p_modify;
  145. }
  146. - (void)ra_uninstall {
  147. if (self.appliedView && self.appliedViewIsView && self.validate && self.constraint) {
  148. UIView *appliedView = ((UIView *)self.appliedView);
  149. if ((self.attribute == NSLayoutAttributeWidth || self.attribute == NSLayoutAttributeHeight) && !self.toView) {
  150. [appliedView removeConstraint:self.constraint];
  151. } else {
  152. [appliedView.superview removeConstraint:self.constraint];
  153. }
  154. }
  155. [self.appendConstraints enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, RAConstraint * _Nonnull obj, BOOL * _Nonnull stop) {
  156. [obj ra_uninstall];
  157. }];
  158. self.validate = NO;
  159. self.constraint = nil;
  160. self.identifier = nil;
  161. self.toView = nil;
  162. self.relation = NSLayoutRelationEqual;
  163. self.toAttribute = NSLayoutAttributeNotAnAttribute;
  164. self.constant = 0;
  165. self.priority = UILayoutPriorityDefaultHigh;
  166. [self.appendConstraints removeAllObjects];
  167. }
  168. - (void)ra_install {
  169. if (self.appliedView && self.appliedViewIsView && self.validate && !self.constraint) {
  170. UIView *appliedView = ((UIView *)self.appliedView);
  171. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:appliedView
  172. attribute:self.attribute
  173. relatedBy:self.relation
  174. toItem:self.toView
  175. attribute:self.toAttribute
  176. multiplier:1
  177. constant:self.constant];
  178. self.constraint = constraint;
  179. self.constraint.priority = self.priority;
  180. self.constraint.identifier = self.identifier;
  181. if ((self.attribute == NSLayoutAttributeWidth || self.attribute == NSLayoutAttributeHeight) && !self.toView) {
  182. [appliedView addConstraint:self.constraint];
  183. } else{
  184. [appliedView.superview addConstraint:self.constraint];
  185. }
  186. }
  187. [self.appendConstraints enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, RAConstraint * _Nonnull obj, BOOL * _Nonnull stop) {
  188. [obj ra_install];
  189. }];
  190. }
  191. - (NSMutableDictionary *)appendConstraints {
  192. if (!_appendConstraints) {
  193. _appendConstraints = [NSMutableDictionary dictionary];
  194. }
  195. return _appendConstraints;
  196. }
  197. - (BOOL)appliedViewIsView {
  198. BOOL isView = [self.appliedView isKindOfClass:[UIView class]];
  199. return isView;
  200. }
  201. @end
  202. #pragma mark - Maker
  203. @interface RAConstraintMaker ()
  204. @property (nonatomic,weak) UIView *appliedView;
  205. @property (nonatomic,strong) RAConstraint *left;
  206. @property (nonatomic,strong) RAConstraint *top;
  207. @property (nonatomic,strong) RAConstraint *right;
  208. @property (nonatomic,strong) RAConstraint *bottom;
  209. @property (nonatomic,strong) RAConstraint *centerX;
  210. @property (nonatomic,strong) RAConstraint *centerY;
  211. @property (nonatomic,strong) RAConstraint *baseLine;
  212. @property (nonatomic,strong) RAConstraint *width;
  213. @property (nonatomic,strong) RAConstraint *height;
  214. @property (nonatomic,strong) RAConstraint *ra_safeAreaLeft API_AVAILABLE(ios(11.0),tvos(11.0));
  215. @property (nonatomic,strong) RAConstraint *ra_safeAreaTop API_AVAILABLE(ios(11.0),tvos(11.0));
  216. @property (nonatomic,strong) RAConstraint *ra_safeAreaRight API_AVAILABLE(ios(11.0),tvos(11.0));
  217. @property (nonatomic,strong) RAConstraint *ra_safeAreaBottom API_AVAILABLE(ios(11.0),tvos(11.0));
  218. @property (nonatomic,strong) NSMutableArray<RAConstraint *> *constraints;
  219. @property (nonatomic,copy) void (^ra_modifyConstraint)(NSString *identifier, CGFloat constant);
  220. @end
  221. static const UILayoutPriority kRALayoutPriorityNone = -1;
  222. @implementation RAConstraintMaker
  223. - (instancetype)initWithAppliedView:(UIView *)view {
  224. if (self = [super init]) {
  225. self.appliedView = view;
  226. [self.constraints addObjectsFromArray:@[
  227. self.left,
  228. self.top,
  229. self.right,
  230. self.bottom,
  231. self.centerX,
  232. self.centerY,
  233. self.baseLine,
  234. self.width,
  235. self.height
  236. ]];
  237. self.ra_horizontalHuggingPriority = kRALayoutPriorityNone;
  238. self.ra_verticalHuggingPriority = kRALayoutPriorityNone;
  239. self.ra_horizontalCompressionResistancePriority = kRALayoutPriorityNone;
  240. self.ra_verticalCompressionResistancePriority = kRALayoutPriorityNone;
  241. __weak typeof(self) weakSelf = self;
  242. self.ra_modifyConstraint = [^(NSString *identifier, CGFloat constant) {
  243. NSDictionary *constraintRes = [weakSelf findConstraintOfView:weakSelf.appliedView byIdentifier:identifier];
  244. if (constraintRes) {
  245. UIView *view = constraintRes[@"view"];
  246. NSLayoutConstraint *constraint = constraintRes[@"constraint"];
  247. constraint.constant = constant;
  248. [view layoutIfNeeded];
  249. }
  250. } copy];
  251. }
  252. return self;
  253. }
  254. - (NSDictionary *)findConstraintOfView:(UIView *)view byIdentifier:(NSString *)identifier {
  255. if (!view || !identifier) {
  256. return nil;
  257. }
  258. NSLayoutConstraint *result = nil;
  259. NSArray<NSLayoutConstraint *> *constraintArray = view.constraints;
  260. for (NSLayoutConstraint *constraint in constraintArray) {
  261. if (constraint.identifier && [constraint.identifier isEqualToString:identifier]) {
  262. result = constraint;
  263. break;
  264. }
  265. }
  266. if (result) {
  267. return @{
  268. @"view" : view,
  269. @"constraint" : result
  270. };
  271. } else {
  272. if (view.superview) {
  273. return [self findConstraintOfView:view.superview byIdentifier:identifier];
  274. } else {
  275. return nil;
  276. }
  277. }
  278. }
  279. - (NSMutableArray<RAConstraint *> *)constraints {
  280. if (!_constraints) {
  281. _constraints = [NSMutableArray array];
  282. }
  283. return _constraints;
  284. }
  285. - (RAConstraint *)left {
  286. if (!_left) {
  287. _left = [RAConstraint new];
  288. _left.attribute = NSLayoutAttributeLeft;
  289. _left.appliedView = self.appliedView;
  290. }
  291. return _left;
  292. }
  293. - (RAConstraint *)top {
  294. if (!_top) {
  295. _top = [RAConstraint new];
  296. _top.attribute = NSLayoutAttributeTop;
  297. _top.appliedView = self.appliedView;
  298. }
  299. return _top;
  300. }
  301. - (RAConstraint *)right {
  302. if (!_right) {
  303. _right = [RAConstraint new];
  304. _right.attribute = NSLayoutAttributeRight;
  305. _right.appliedView = self.appliedView;
  306. }
  307. return _right;
  308. }
  309. - (RAConstraint *)bottom {
  310. if (!_bottom) {
  311. _bottom = [RAConstraint new];
  312. _bottom.attribute = NSLayoutAttributeBottom;
  313. _bottom.appliedView = self.appliedView;
  314. }
  315. return _bottom;
  316. }
  317. - (RAConstraint *)centerX {
  318. if (!_centerX) {
  319. _centerX = [RAConstraint new];
  320. _centerX.attribute = NSLayoutAttributeCenterX;
  321. _centerX.appliedView = self.appliedView;
  322. }
  323. return _centerX;
  324. }
  325. - (RAConstraint *)centerY {
  326. if (!_centerY) {
  327. _centerY = [RAConstraint new];
  328. _centerY.attribute = NSLayoutAttributeCenterY;
  329. _centerY.appliedView = self.appliedView;
  330. }
  331. return _centerY;
  332. }
  333. - (RAConstraint *)baseLine {
  334. if (!_baseLine) {
  335. _baseLine = [RAConstraint new];
  336. _baseLine.attribute = NSLayoutAttributeBaseline;
  337. _baseLine.appliedView = self.appliedView;
  338. }
  339. return _baseLine;
  340. }
  341. - (RAConstraint *)width {
  342. if (!_width) {
  343. _width = [RAConstraint new];
  344. _width.attribute = NSLayoutAttributeWidth;
  345. _width.appliedView = self.appliedView;
  346. }
  347. return _width;
  348. }
  349. - (RAConstraint *)height {
  350. if (!_height) {
  351. _height = [RAConstraint new];
  352. _height.attribute = NSLayoutAttributeHeight;
  353. _height.appliedView = self.appliedView;
  354. }
  355. return _height;
  356. }
  357. // safe area
  358. - (RAConstraint *)ra_safeAreaLeft {
  359. if (!_ra_safeAreaLeft) {
  360. _ra_safeAreaLeft = [RAConstraint new];
  361. _ra_safeAreaLeft.attribute = NSLayoutAttributeLeft;
  362. _ra_safeAreaLeft.appliedView = self.appliedView.safeAreaLayoutGuide;
  363. }
  364. return _ra_safeAreaLeft;
  365. }
  366. - (RAConstraint *)ra_safeAreaTop {
  367. if (!_ra_safeAreaTop) {
  368. _ra_safeAreaTop = [RAConstraint new];
  369. _ra_safeAreaTop.attribute = NSLayoutAttributeTop;
  370. _ra_safeAreaTop.appliedView = self.appliedView.safeAreaLayoutGuide;
  371. }
  372. return _ra_safeAreaTop;
  373. }
  374. - (RAConstraint *)ra_safeAreaRight {
  375. if (!_ra_safeAreaRight) {
  376. _ra_safeAreaRight = [RAConstraint new];
  377. _ra_safeAreaRight.attribute = NSLayoutAttributeRight;
  378. _ra_safeAreaRight.appliedView = self.appliedView.safeAreaLayoutGuide;
  379. }
  380. return _ra_safeAreaRight;
  381. }
  382. - (RAConstraint *)ra_safeAreaBottom {
  383. if (!_ra_safeAreaBottom) {
  384. _ra_safeAreaBottom = [RAConstraint new];
  385. _ra_safeAreaBottom.attribute = NSLayoutAttributeBottom;
  386. _ra_safeAreaBottom.appliedView = self.appliedView.safeAreaLayoutGuide;
  387. }
  388. return _ra_safeAreaBottom;
  389. }
  390. - (void)ra_install {
  391. if (self.appliedView) {
  392. // 抗拉伸
  393. if (self.ra_horizontalHuggingPriority != kRALayoutPriorityNone) {
  394. [self.appliedView setContentHuggingPriority:self.ra_horizontalHuggingPriority forAxis:UILayoutConstraintAxisHorizontal];
  395. }
  396. if (self.ra_verticalHuggingPriority != kRALayoutPriorityNone) {
  397. [self.appliedView setContentHuggingPriority:self.ra_verticalHuggingPriority forAxis:UILayoutConstraintAxisVertical];
  398. }
  399. // 抗压缩
  400. if (self.ra_horizontalCompressionResistancePriority != kRALayoutPriorityNone) {
  401. [self.appliedView setContentCompressionResistancePriority:self.ra_horizontalCompressionResistancePriority forAxis:UILayoutConstraintAxisHorizontal];
  402. }
  403. if (self.ra_verticalCompressionResistancePriority != kRALayoutPriorityNone) {
  404. [self.appliedView setContentCompressionResistancePriority:self.ra_verticalCompressionResistancePriority forAxis:UILayoutConstraintAxisVertical];
  405. }
  406. // normal constraint
  407. for (RAConstraint *constraint in self.constraints) {
  408. [constraint ra_install];
  409. }
  410. }
  411. }
  412. - (void)ra_uninstall {
  413. if (self.appliedView) {
  414. for (RAConstraint *constraint in self.constraints) {
  415. [constraint ra_uninstall];
  416. }
  417. }
  418. }
  419. @end
  420. #pragma mark - UIView
  421. static const char RAMaker = '\a';
  422. @implementation UIView (RAConstraint)
  423. - (void)setRA_Maker:(RAConstraintMaker *)maker {
  424. if (maker) {
  425. objc_setAssociatedObject(self, &RAMaker, maker, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  426. }
  427. }
  428. - (RAConstraintMaker *)ra_maker {
  429. RAConstraintMaker *maker = objc_getAssociatedObject(self, &RAMaker);
  430. if (maker == nil) {
  431. maker = [[RAConstraintMaker alloc] initWithAppliedView:self];
  432. [self setRA_Maker:maker];
  433. }
  434. return maker;
  435. }
  436. - (void)ra_applyConstraints:(void(^)(RAConstraintMaker *maker))apply {
  437. if (self.superview) {
  438. // self.superview.translatesAutoresizingMaskIntoConstraints = NO;
  439. self.translatesAutoresizingMaskIntoConstraints = NO;
  440. RAConstraintMaker *maker = [self ra_maker];
  441. if (apply) {
  442. apply(maker);
  443. }
  444. [self applyMaker:maker];
  445. }
  446. }
  447. - (void)applyMaker:(RAConstraintMaker *)maker {
  448. if (maker) {
  449. [self setRA_Maker:maker];
  450. [maker ra_install];
  451. [self.superview layoutIfNeeded];
  452. }
  453. }
  454. - (void)ra_remakeConstraints:(void(^)(RAConstraintMaker *maker))remake {
  455. [[self ra_maker] ra_uninstall];
  456. [self ra_applyConstraints:remake];
  457. }
  458. #pragma mark - P
  459. - (RAConstraint *)left {
  460. return [self ra_maker].left;
  461. }
  462. - (RAConstraint *)top {
  463. return [self ra_maker].top;
  464. }
  465. - (RAConstraint *)right {
  466. return [self ra_maker].right;
  467. }
  468. - (RAConstraint *)bottom {
  469. return [self ra_maker].bottom;
  470. }
  471. - (RAConstraint *)centerX {
  472. return [self ra_maker].centerX;
  473. }
  474. - (RAConstraint *)centerY {
  475. return [self ra_maker].centerY;
  476. }
  477. - (RAConstraint *)baseLine {
  478. return [self ra_maker].baseLine;
  479. }
  480. - (RAConstraint *)width {
  481. return [self ra_maker].width;
  482. }
  483. - (RAConstraint *)height {
  484. return [self ra_maker].height;
  485. }
  486. // safe area
  487. - (RAConstraint *)ra_safeAreaLeft {
  488. return [self ra_maker].ra_safeAreaLeft;
  489. }
  490. - (RAConstraint *)ra_safeAreaTop {
  491. return [self ra_maker].ra_safeAreaTop;
  492. }
  493. - (RAConstraint *)ra_safeAreaRight {
  494. return [self ra_maker].ra_safeAreaRight;
  495. }
  496. - (RAConstraint *)ra_safeAreaBottom {
  497. return [self ra_maker].ra_safeAreaBottom;
  498. }
  499. // modify
  500. - (void (^)(NSString *, CGFloat))ra_modifyConstraint {
  501. return [self ra_maker].ra_modifyConstraint;
  502. }
  503. @end
  504. #pragma mark - View Controller
  505. #pragma clang diagnostic push
  506. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  507. @implementation UIViewController (RAConstraint)
  508. - (RAConstraint *)ra_topLayoutGuid {
  509. RAConstraint *constraint = [RAConstraint new];
  510. constraint.attribute = NSLayoutAttributeBottom;
  511. constraint.appliedView = self.topLayoutGuide;
  512. return constraint;
  513. }
  514. - (RAConstraint *)ra_topLayoutGuidTop {
  515. RAConstraint *constraint = [RAConstraint new];
  516. constraint.attribute = NSLayoutAttributeTop;
  517. constraint.appliedView = self.topLayoutGuide;
  518. return constraint;
  519. }
  520. - (RAConstraint *)ra_topLayoutGuidBottom {
  521. RAConstraint *constraint = [RAConstraint new];
  522. constraint.attribute = NSLayoutAttributeBottom;
  523. constraint.appliedView = self.topLayoutGuide;
  524. return constraint;
  525. }
  526. - (RAConstraint *)ra_bottomLayoutGuid {
  527. RAConstraint *constraint = [RAConstraint new];
  528. constraint.attribute = NSLayoutAttributeTop;
  529. constraint.appliedView = self.bottomLayoutGuide;
  530. return constraint;
  531. }
  532. - (RAConstraint *)ra_bottomLayoutGuidTop {
  533. RAConstraint *constraint = [RAConstraint new];
  534. constraint.attribute = NSLayoutAttributeTop;
  535. constraint.appliedView = self.bottomLayoutGuide;
  536. return constraint;
  537. }
  538. - (RAConstraint *)ra_bottomLayoutGuidBottom {
  539. RAConstraint *constraint = [RAConstraint new];
  540. constraint.attribute = NSLayoutAttributeBottom;
  541. constraint.appliedView = self.bottomLayoutGuide;
  542. return constraint;
  543. }
  544. #pragma clang diagnostic pop
  545. @end