口袋优选

UIView+SDAutoLayout.m 39KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. //
  2. // UIView+SDAutoLayout.m
  3. //
  4. // Created by gsd on 15/10/6.
  5. // Copyright (c) 2015年 gsd. All rights reserved.
  6. //
  7. /*
  8. *********************************************************************************
  9. * *
  10. * 在您使用此自动布局库的过程中如果出现bug请及时以以下任意一种方式联系我们,我们会及时修复bug并 *
  11. * 帮您解决问题。 *
  12. * QQ : 2689718696(gsdios) *
  13. * Email : gsdios@126.com *
  14. * GitHub: https://github.com/gsdios *
  15. * 新浪微博:GSD_iOS *
  16. * *
  17. *********************************************************************************
  18. */
  19. #import "UIView+SDAutoLayout.h"
  20. #import "UITableView+SDAutoTableViewCellHeight.h"
  21. #import <objc/runtime.h>
  22. @interface SDAutoLayoutModel ()
  23. @property (nonatomic, strong) SDAutoLayoutModelItem *width;
  24. @property (nonatomic, strong) SDAutoLayoutModelItem *height;
  25. @property (nonatomic, strong) SDAutoLayoutModelItem *left;
  26. @property (nonatomic, strong) SDAutoLayoutModelItem *top;
  27. @property (nonatomic, strong) SDAutoLayoutModelItem *right;
  28. @property (nonatomic, strong) SDAutoLayoutModelItem *bottom;
  29. @property (nonatomic, strong) NSNumber *centerX;
  30. @property (nonatomic, strong) NSNumber *centerY;
  31. @property (nonatomic, strong) NSNumber *maxWidth;
  32. @property (nonatomic, strong) NSNumber *maxHeight;
  33. @property (nonatomic, strong) NSNumber *minWidth;
  34. @property (nonatomic, strong) NSNumber *minHeight;
  35. @property (nonatomic, strong) SDAutoLayoutModelItem *ratio_width;
  36. @property (nonatomic, strong) SDAutoLayoutModelItem *ratio_height;
  37. @property (nonatomic, strong) SDAutoLayoutModelItem *ratio_left;
  38. @property (nonatomic, strong) SDAutoLayoutModelItem *ratio_top;
  39. @property (nonatomic, strong) SDAutoLayoutModelItem *ratio_right;
  40. @property (nonatomic, strong) SDAutoLayoutModelItem *ratio_bottom;
  41. @property (nonatomic, strong) SDAutoLayoutModelItem *equalLeft;
  42. @property (nonatomic, strong) SDAutoLayoutModelItem *equalRight;
  43. @property (nonatomic, strong) SDAutoLayoutModelItem *equalTop;
  44. @property (nonatomic, strong) SDAutoLayoutModelItem *equalBottom;
  45. @property (nonatomic, strong) SDAutoLayoutModelItem *equalCenterX;
  46. @property (nonatomic, strong) SDAutoLayoutModelItem *equalCenterY;
  47. @property (nonatomic, strong) SDAutoLayoutModelItem *widthEqualHeight;
  48. @property (nonatomic, strong) SDAutoLayoutModelItem *heightEqualWidth;
  49. @end
  50. @implementation SDAutoLayoutModel
  51. @synthesize leftSpaceToView = _leftSpaceToView;
  52. @synthesize rightSpaceToView = _rightSpaceToView;
  53. @synthesize topSpaceToView = _topSpaceToView;
  54. @synthesize bottomSpaceToView = _bottomSpaceToView;
  55. @synthesize widthIs = _widthIs;
  56. @synthesize heightIs = _heightIs;
  57. @synthesize widthRatioToView = _widthRatioToView;
  58. @synthesize heightRatioToView = _heightRatioToView;
  59. @synthesize leftEqualToView = _leftEqualToView;
  60. @synthesize rightEqualToView = _rightEqualToView;
  61. @synthesize topEqualToView = _topEqualToView;
  62. @synthesize bottomEqualToView = _bottomEqualToView;
  63. @synthesize centerXEqualToView = _centerXEqualToView;
  64. @synthesize centerYEqualToView = _centerYEqualToView;
  65. @synthesize xIs = _xIs;
  66. @synthesize yIs = _yIs;
  67. @synthesize centerXIs = _centerXIs;
  68. @synthesize centerYIs = _centerYIs;
  69. @synthesize autoHeightRatio = _autoHeightRatio;
  70. @synthesize spaceToSuperView = _spaceToSuperView;
  71. @synthesize maxWidthIs = _maxWidthIs;
  72. @synthesize maxHeightIs = _maxHeightIs;
  73. @synthesize minWidthIs = _minWidthIs;
  74. @synthesize minHeightIs = _minHeightIs;
  75. @synthesize widthEqualToHeight = _widthEqualToHeight;
  76. @synthesize heightEqualToWidth = _heightEqualToWidth;
  77. - (MarginToView)leftSpaceToView
  78. {
  79. if (!_leftSpaceToView) {
  80. _leftSpaceToView = [self marginToViewblockWithKey:@"left"];
  81. }
  82. return _leftSpaceToView;
  83. }
  84. - (MarginToView)rightSpaceToView
  85. {
  86. if (!_rightSpaceToView) {
  87. _rightSpaceToView = [self marginToViewblockWithKey:@"right"];
  88. }
  89. return _rightSpaceToView;
  90. }
  91. - (MarginToView)topSpaceToView
  92. {
  93. if (!_topSpaceToView) {
  94. _topSpaceToView = [self marginToViewblockWithKey:@"top"];
  95. }
  96. return _topSpaceToView;
  97. }
  98. - (MarginToView)bottomSpaceToView
  99. {
  100. if (!_bottomSpaceToView) {
  101. _bottomSpaceToView = [self marginToViewblockWithKey:@"bottom"];
  102. }
  103. return _bottomSpaceToView;
  104. }
  105. - (MarginToView)marginToViewblockWithKey:(NSString *)key
  106. {
  107. __weak typeof(self) weakSelf = self;
  108. return ^(UIView *view, CGFloat value) {
  109. SDAutoLayoutModelItem *item = [SDAutoLayoutModelItem new];
  110. item.value = @(value);
  111. item.refView = view;
  112. [weakSelf setValue:item forKey:key];
  113. return weakSelf;
  114. };
  115. }
  116. - (WidthHeight)widthIs
  117. {
  118. if (!_widthIs) {
  119. __weak typeof(self) weakSelf = self;
  120. _widthIs = ^(CGFloat value) {
  121. weakSelf.needsAutoResizeView.width = value;
  122. weakSelf.needsAutoResizeView.fixedWith = @(value);
  123. return weakSelf;
  124. };
  125. }
  126. return _widthIs;
  127. }
  128. - (WidthHeight)heightIs
  129. {
  130. if (!_heightIs) {
  131. __weak typeof(self) weakSelf = self;
  132. _heightIs = ^(CGFloat value) {
  133. weakSelf.needsAutoResizeView.height = value;
  134. weakSelf.needsAutoResizeView.fixedHeight = @(value);
  135. return weakSelf;
  136. };
  137. }
  138. return _heightIs;
  139. }
  140. - (WidthHeightEqualToView)widthRatioToView
  141. {
  142. if (!_widthRatioToView) {
  143. __weak typeof(self) weakSelf = self;
  144. _widthRatioToView = ^(UIView *view, CGFloat value) {
  145. weakSelf.ratio_width = [SDAutoLayoutModelItem new];
  146. weakSelf.ratio_width.value = @(value);
  147. weakSelf.ratio_width.refView = view;
  148. return weakSelf;
  149. };
  150. }
  151. return _widthRatioToView;
  152. }
  153. - (WidthHeightEqualToView)heightRatioToView
  154. {
  155. if (!_heightRatioToView) {
  156. __weak typeof(self) weakSelf = self;
  157. _heightRatioToView = ^(UIView *view, CGFloat value) {
  158. weakSelf.ratio_height = [SDAutoLayoutModelItem new];
  159. weakSelf.ratio_height.refView = view;
  160. weakSelf.ratio_height.value = @(value);
  161. return weakSelf;
  162. };
  163. }
  164. return _heightRatioToView;
  165. }
  166. - (WidthHeight)maxWidthIs
  167. {
  168. if (!_maxWidthIs) {
  169. _maxWidthIs = [self limitingWidthHeightWithKey:@"maxWidth"];
  170. }
  171. return _maxWidthIs;
  172. }
  173. - (WidthHeight)maxHeightIs
  174. {
  175. if (!_maxHeightIs) {
  176. _maxHeightIs = [self limitingWidthHeightWithKey:@"maxHeight"];
  177. }
  178. return _maxHeightIs;
  179. }
  180. - (WidthHeight)minWidthIs
  181. {
  182. if (!_minWidthIs) {
  183. _minWidthIs = [self limitingWidthHeightWithKey:@"minWidth"];
  184. }
  185. return _minWidthIs;
  186. }
  187. - (WidthHeight)minHeightIs
  188. {
  189. if (!_minHeightIs) {
  190. _minHeightIs = [self limitingWidthHeightWithKey:@"minHeight"];
  191. }
  192. return _minHeightIs;
  193. }
  194. - (WidthHeight)limitingWidthHeightWithKey:(NSString *)key
  195. {
  196. __weak typeof(self) weakSelf = self;
  197. return ^(CGFloat value) {
  198. [weakSelf setValue:@(value) forKey:key];
  199. return weakSelf;
  200. };
  201. }
  202. - (MarginEqualToView)marginEqualToViewBlockWithKey:(NSString *)key
  203. {
  204. __weak typeof(self) weakSelf = self;
  205. return ^(UIView *view) {
  206. SDAutoLayoutModelItem *item = [SDAutoLayoutModelItem new];
  207. item.refView = view;
  208. [weakSelf setValue:item forKey:key];
  209. return weakSelf;
  210. };
  211. }
  212. - (MarginEqualToView)leftEqualToView
  213. {
  214. if (!_leftEqualToView) {
  215. _leftEqualToView = [self marginEqualToViewBlockWithKey:@"equalLeft"];
  216. }
  217. return _leftEqualToView;
  218. }
  219. - (MarginEqualToView)rightEqualToView
  220. {
  221. if (!_rightEqualToView) {
  222. _rightEqualToView = [self marginEqualToViewBlockWithKey:@"equalRight"];
  223. }
  224. return _rightEqualToView;
  225. }
  226. - (MarginEqualToView)topEqualToView
  227. {
  228. if (!_topEqualToView) {
  229. _topEqualToView = [self marginEqualToViewBlockWithKey:@"equalTop"];
  230. }
  231. return _topEqualToView;
  232. }
  233. - (MarginEqualToView)bottomEqualToView
  234. {
  235. if (!_bottomEqualToView) {
  236. _bottomEqualToView = [self marginEqualToViewBlockWithKey:@"equalBottom"];
  237. }
  238. return _bottomEqualToView;
  239. }
  240. - (MarginEqualToView)centerXEqualToView
  241. {
  242. if (!_centerXEqualToView) {
  243. _centerXEqualToView = [self marginEqualToViewBlockWithKey:@"equalCenterX"];
  244. }
  245. return _centerXEqualToView;
  246. }
  247. - (MarginEqualToView)centerYEqualToView
  248. {
  249. if (!_centerYEqualToView) {
  250. _centerYEqualToView = [self marginEqualToViewBlockWithKey:@"equalCenterY"];
  251. }
  252. return _centerYEqualToView;
  253. }
  254. - (Margin)marginBlockWithKey:(NSString *)key
  255. {
  256. __weak typeof(self) weakSelf = self;
  257. return ^(CGFloat value) {
  258. if ([key isEqualToString:@"x"]) {
  259. weakSelf.needsAutoResizeView.left = value;
  260. } else if ([key isEqualToString:@"y"]) {
  261. weakSelf.needsAutoResizeView.top = value;
  262. } else if ([key isEqualToString:@"centerX"]) {
  263. weakSelf.centerX = @(value);
  264. } else if ([key isEqualToString:@"centerY"]) {
  265. weakSelf.centerY = @(value);
  266. }
  267. return weakSelf;
  268. };
  269. }
  270. - (Margin)xIs
  271. {
  272. if (!_xIs) {
  273. _xIs = [self marginBlockWithKey:@"x"];
  274. }
  275. return _xIs;
  276. }
  277. - (Margin)yIs
  278. {
  279. if (!_yIs) {
  280. _yIs = [self marginBlockWithKey:@"y"];
  281. }
  282. return _yIs;
  283. }
  284. - (Margin)centerXIs
  285. {
  286. if (!_centerXIs) {
  287. _centerXIs = [self marginBlockWithKey:@"centerX"];
  288. }
  289. return _centerXIs;
  290. }
  291. - (Margin)centerYIs
  292. {
  293. if (!_centerYIs) {
  294. _centerYIs = [self marginBlockWithKey:@"centerY"];
  295. }
  296. return _centerYIs;
  297. }
  298. - (AutoHeight)autoHeightRatio
  299. {
  300. __weak typeof(self) weakSelf = self;
  301. if (!_autoHeightRatio) {
  302. _autoHeightRatio = ^(CGFloat ratioaValue) {
  303. weakSelf.needsAutoResizeView.autoHeightRatioValue = @(ratioaValue);
  304. return weakSelf;
  305. };
  306. }
  307. return _autoHeightRatio;
  308. }
  309. - (SpaceToSuperView)spaceToSuperView
  310. {
  311. __weak typeof(self) weakSelf = self;
  312. if (!_spaceToSuperView) {
  313. _spaceToSuperView = ^(UIEdgeInsets insets) {
  314. UIView *superView = weakSelf.needsAutoResizeView.superview;
  315. if (superView) {
  316. weakSelf.needsAutoResizeView.sd_layout
  317. .leftSpaceToView(superView, insets.left)
  318. .topSpaceToView(superView, insets.top)
  319. .rightSpaceToView(superView, insets.right)
  320. .bottomSpaceToView(superView, insets.bottom);
  321. }
  322. };
  323. }
  324. return _spaceToSuperView;
  325. }
  326. - (SameWidthHeight)widthEqualToHeight
  327. {
  328. __weak typeof(self) weakSelf = self;
  329. if (!_widthEqualToHeight) {
  330. _widthEqualToHeight = ^() {
  331. weakSelf.widthEqualHeight = [SDAutoLayoutModelItem new];
  332. // 主动触发一次赋值操作
  333. weakSelf.needsAutoResizeView.height = weakSelf.needsAutoResizeView.height;
  334. return weakSelf;
  335. };
  336. }
  337. return _widthEqualToHeight;
  338. }
  339. - (SameWidthHeight)heightEqualToWidth
  340. {
  341. __weak typeof(self) weakSelf = self;
  342. if (!_heightEqualToWidth) {
  343. _heightEqualToWidth = ^() {
  344. weakSelf.heightEqualWidth = [SDAutoLayoutModelItem new];
  345. // 主动触发一次赋值操作
  346. weakSelf.needsAutoResizeView.width = weakSelf.needsAutoResizeView.width;
  347. return weakSelf;
  348. };
  349. }
  350. return _heightEqualToWidth;
  351. }
  352. @end
  353. @implementation UIView (SDAutoHeightWidth)
  354. - (SDUIViewCategoryManager *)sd_categoryManager
  355. {
  356. SDUIViewCategoryManager *manager = objc_getAssociatedObject(self, _cmd);
  357. if (!manager) {
  358. objc_setAssociatedObject(self, _cmd, [SDUIViewCategoryManager new], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  359. }
  360. return objc_getAssociatedObject(self, _cmd);
  361. }
  362. - (void)setupAutoHeightWithBottomView:(UIView *)bottomView bottomMargin:(CGFloat)bottomMargin
  363. {
  364. if (!bottomView) return;
  365. [self setupAutoHeightWithBottomViewsArray:@[bottomView] bottomMargin:bottomMargin];
  366. }
  367. - (void)setupAutoWidthWithRightView:(UIView *)rightView rightMargin:(CGFloat)rightMargin
  368. {
  369. if (!rightView) return;
  370. self.sd_rightViewsArray = @[rightView];
  371. self.sd_rightViewRightMargin = rightMargin;
  372. }
  373. - (void)setupAutoHeightWithBottomViewsArray:(NSArray *)bottomViewsArray bottomMargin:(CGFloat)bottomMargin
  374. {
  375. if (!bottomViewsArray) return;
  376. // 清空之前的view
  377. [self.sd_bottomViewsArray removeAllObjects];
  378. [self.sd_bottomViewsArray addObjectsFromArray:bottomViewsArray];
  379. self.sd_bottomViewBottomMargin = bottomMargin;
  380. }
  381. - (void)clearAutoHeigtSettings
  382. {
  383. [self.sd_bottomViewsArray removeAllObjects];
  384. }
  385. - (void)clearAutoWidthSettings
  386. {
  387. self.sd_rightViewsArray = nil;
  388. }
  389. - (void)updateLayout
  390. {
  391. [self.superview layoutSubviews];
  392. }
  393. - (CGFloat)autoHeight
  394. {
  395. return [objc_getAssociatedObject(self, _cmd) floatValue];
  396. }
  397. - (void)setAutoHeight:(CGFloat)autoHeight
  398. {
  399. objc_setAssociatedObject(self, @selector(autoHeight), @(autoHeight), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  400. }
  401. - (NSMutableArray *)sd_bottomViewsArray
  402. {
  403. NSMutableArray *array = objc_getAssociatedObject(self, _cmd);
  404. if (!array) {
  405. objc_setAssociatedObject(self, _cmd, [NSMutableArray new], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  406. }
  407. return objc_getAssociatedObject(self, _cmd);
  408. }
  409. - (NSArray *)sd_rightViewsArray
  410. {
  411. return [[self sd_categoryManager] rightViewsArray];
  412. }
  413. - (void)setSd_rightViewsArray:(NSArray *)sd_rightViewsArray
  414. {
  415. [[self sd_categoryManager] setRightViewsArray:sd_rightViewsArray];
  416. }
  417. - (CGFloat)sd_bottomViewBottomMargin
  418. {
  419. return [objc_getAssociatedObject(self, _cmd) floatValue];
  420. }
  421. - (void)setSd_bottomViewBottomMargin:(CGFloat)sd_bottomViewBottomMargin
  422. {
  423. objc_setAssociatedObject(self, @selector(sd_bottomViewBottomMargin), @(sd_bottomViewBottomMargin), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  424. }
  425. - (void)setSd_rightViewRightMargin:(CGFloat)sd_rightViewRightMargin
  426. {
  427. [[self sd_categoryManager] setRightViewRightMargin:sd_rightViewRightMargin];
  428. }
  429. - (CGFloat)sd_rightViewRightMargin
  430. {
  431. return [[self sd_categoryManager] rightViewRightMargin];
  432. }
  433. @end
  434. @implementation UIView (SDLayoutExtention)
  435. - (void (^)(CGRect))didFinishAutoLayoutBlock
  436. {
  437. return objc_getAssociatedObject(self, _cmd);
  438. }
  439. - (void)setDidFinishAutoLayoutBlock:(void (^)(CGRect))didFinishAutoLayoutBlock
  440. {
  441. objc_setAssociatedObject(self, @selector(didFinishAutoLayoutBlock), didFinishAutoLayoutBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
  442. }
  443. - (NSNumber *)sd_cornerRadius
  444. {
  445. return objc_getAssociatedObject(self, _cmd);
  446. }
  447. - (void)setSd_cornerRadius:(NSNumber *)sd_cornerRadius
  448. {
  449. objc_setAssociatedObject(self, @selector(sd_cornerRadius), sd_cornerRadius, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  450. }
  451. - (NSNumber *)sd_cornerRadiusFromWidthRatio
  452. {
  453. return objc_getAssociatedObject(self, _cmd);
  454. }
  455. - (void)setSd_cornerRadiusFromWidthRatio:(NSNumber *)sd_cornerRadiusFromWidthRatio
  456. {
  457. objc_setAssociatedObject(self, @selector(sd_cornerRadiusFromWidthRatio), sd_cornerRadiusFromWidthRatio, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  458. }
  459. - (NSNumber *)sd_cornerRadiusFromHeightRatio
  460. {
  461. return objc_getAssociatedObject(self, _cmd);
  462. }
  463. - (void)setSd_cornerRadiusFromHeightRatio:(NSNumber *)sd_cornerRadiusFromHeightRatio
  464. {
  465. objc_setAssociatedObject(self, @selector(sd_cornerRadiusFromHeightRatio), sd_cornerRadiusFromHeightRatio, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  466. }
  467. - (NSArray *)sd_equalWidthSubviews
  468. {
  469. return objc_getAssociatedObject(self, _cmd);
  470. }
  471. - (void)setSd_equalWidthSubviews:(NSArray *)sd_equalWidthSubviews
  472. {
  473. objc_setAssociatedObject(self, @selector(sd_equalWidthSubviews), sd_equalWidthSubviews, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  474. }
  475. - (void)sd_addSubviews:(NSArray *)subviews
  476. {
  477. [subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
  478. if ([view isKindOfClass:[UIView class]]) {
  479. [self addSubview:view];
  480. }
  481. }];
  482. }
  483. @end
  484. @implementation UIScrollView (SDAutoContentSize)
  485. - (void)setupAutoContentSizeWithBottomView:(UIView *)bottomView bottomMargin:(CGFloat)bottomMargin
  486. {
  487. [self setupAutoHeightWithBottomView:bottomView bottomMargin:bottomMargin];
  488. }
  489. - (void)setupAutoContentSizeWithRightView:(UIView *)rightView rightMargin:(CGFloat)rightMargin
  490. {
  491. if (!rightView) return;
  492. self.sd_rightViewsArray = @[rightView];
  493. self.sd_rightViewRightMargin = rightMargin;
  494. }
  495. @end
  496. @implementation UILabel (SDLabelAutoResize)
  497. - (BOOL)isAttributedContent
  498. {
  499. return [objc_getAssociatedObject(self, _cmd) boolValue];
  500. }
  501. - (void)setIsAttributedContent:(BOOL)isAttributedContent
  502. {
  503. objc_setAssociatedObject(self, @selector(isAttributedContent), @(isAttributedContent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  504. }
  505. - (void)setSingleLineAutoResizeWithMaxWidth:(CGFloat)maxWidth
  506. {
  507. self.sd_maxWidth = @(maxWidth);
  508. }
  509. @end
  510. @implementation SDAutoLayoutModelItem
  511. @end
  512. @implementation UIView (SDAutoLayout)
  513. + (void)load
  514. {
  515. static dispatch_once_t onceToken;
  516. dispatch_once(&onceToken, ^{
  517. NSArray *selStringsArray = @[@"layoutSubviews"];
  518. [selStringsArray enumerateObjectsUsingBlock:^(NSString *selString, NSUInteger idx, BOOL *stop) {
  519. NSString *mySelString = [@"sd_" stringByAppendingString:selString];
  520. Method originalMethod = class_getInstanceMethod(self, NSSelectorFromString(selString));
  521. Method myMethod = class_getInstanceMethod(self, NSSelectorFromString(mySelString));
  522. method_exchangeImplementations(originalMethod, myMethod);
  523. }];
  524. });
  525. }
  526. #pragma mark - properties
  527. - (NSMutableArray *)autoLayoutModelsArray
  528. {
  529. if (!objc_getAssociatedObject(self, _cmd)) {
  530. objc_setAssociatedObject(self, _cmd, [NSMutableArray array], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  531. }
  532. return objc_getAssociatedObject(self, _cmd);
  533. }
  534. - (NSNumber *)fixedWith
  535. {
  536. return objc_getAssociatedObject(self, _cmd);
  537. }
  538. - (void)setFixedWith:(NSNumber *)fixedWith
  539. {
  540. objc_setAssociatedObject(self, @selector(fixedWith), fixedWith, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  541. }
  542. - (NSNumber *)fixedHeight
  543. {
  544. return objc_getAssociatedObject(self, _cmd);
  545. }
  546. - (void)setFixedHeight:(NSNumber *)fixedHeight
  547. {
  548. objc_setAssociatedObject(self, @selector(fixedHeight), fixedHeight, OBJC_ASSOCIATION_RETAIN);
  549. }
  550. - (NSNumber *)autoHeightRatioValue
  551. {
  552. return objc_getAssociatedObject(self, _cmd);
  553. }
  554. - (void)setAutoHeightRatioValue:(NSNumber *)autoHeightRatioValue
  555. {
  556. objc_setAssociatedObject(self, @selector(autoHeightRatioValue), autoHeightRatioValue, OBJC_ASSOCIATION_RETAIN);
  557. }
  558. - (NSNumber *)sd_maxWidth
  559. {
  560. return objc_getAssociatedObject(self, _cmd);
  561. }
  562. - (void)setSd_maxWidth:(NSNumber *)sd_maxWidth
  563. {
  564. objc_setAssociatedObject(self, @selector(sd_maxWidth), sd_maxWidth, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  565. }
  566. - (SDAutoLayoutModel *)ownLayoutModel
  567. {
  568. return objc_getAssociatedObject(self, _cmd);
  569. }
  570. - (void)setOwnLayoutModel:(SDAutoLayoutModel *)ownLayoutModel
  571. {
  572. objc_setAssociatedObject(self, @selector(ownLayoutModel), ownLayoutModel, OBJC_ASSOCIATION_RETAIN);
  573. }
  574. - (SDAutoLayoutModel *)sd_layout
  575. {
  576. #ifdef SDDebugWithAssert
  577. /*
  578. 卡在这里说明你的要自动布局的view在没有添加到父view的情况下就开始设置布局,你需要这样:
  579. 1. UIView *view = [UIView new];
  580. 2. [superView addSubview:view];
  581. 3. view.sd_layout
  582. .leftEqualToView()...
  583. */
  584. NSAssert(self.superview, @">>>>>>>>>在加入父view之后才可以做自动布局设置");
  585. #endif
  586. SDAutoLayoutModel *model = [self ownLayoutModel];
  587. if (!model) {
  588. model = [SDAutoLayoutModel new];
  589. model.needsAutoResizeView = self;
  590. [self setOwnLayoutModel:model];
  591. [self.superview.autoLayoutModelsArray addObject:model];
  592. }
  593. return model;
  594. }
  595. - (SDAutoLayoutModel *)sd_resetLayout
  596. {
  597. [self sd_clearAutoLayoutSettings];
  598. return [self sd_layout];
  599. }
  600. - (void)sd_clearAutoLayoutSettings
  601. {
  602. SDAutoLayoutModel *model = [self ownLayoutModel];
  603. if (model) {
  604. [self.superview.autoLayoutModelsArray removeObject:model];
  605. [self setOwnLayoutModel:nil];
  606. }
  607. if (self.autoHeightRatioValue) {
  608. self.autoHeightRatioValue = nil;
  609. }
  610. }
  611. - (void)sd_layoutSubviews
  612. {
  613. [self sd_layoutSubviews];
  614. if (self.sd_equalWidthSubviews.count) {
  615. __block CGFloat totalMargin = 0;
  616. [self.sd_equalWidthSubviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
  617. SDAutoLayoutModel *model = view.sd_layout;
  618. CGFloat left = model.left ? [model.left.value floatValue] : model.needsAutoResizeView.left;
  619. totalMargin += (left + [model.right.value floatValue]);
  620. }];
  621. CGFloat averageWidth = (self.width - totalMargin) / self.sd_equalWidthSubviews.count;
  622. [self.sd_equalWidthSubviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
  623. view.width = averageWidth;
  624. view.fixedWith = @(averageWidth);
  625. }];
  626. }
  627. if (self.autoLayoutModelsArray.count) {
  628. [self.autoLayoutModelsArray enumerateObjectsUsingBlock:^(SDAutoLayoutModel *model, NSUInteger idx, BOOL *stop) {
  629. [self sd_resizeWithModel:model];
  630. }];
  631. }
  632. if (self.tag == kSDModelCellTag && [self isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) {
  633. UITableViewCell *cell = (UITableViewCell *)(self.superview);
  634. if ([cell isKindOfClass:NSClassFromString(@"UITableViewCellScrollView")]) {
  635. cell = (UITableViewCell *)cell.superview;
  636. }
  637. if ([cell isKindOfClass:[UITableViewCell class]]) {
  638. CGFloat height = 0;
  639. for (UIView *view in cell.sd_bottomViewsArray) {
  640. height = MAX(height, view.bottom);
  641. }
  642. cell.autoHeight = height + cell.sd_bottomViewBottomMargin;
  643. }
  644. } else if (![self isKindOfClass:[UITableViewCell class]] && (self.sd_bottomViewsArray.count || self.sd_rightViewsArray.count)) {
  645. CGFloat contentHeight = 0;
  646. CGFloat contentWidth = 0;
  647. if (self.sd_bottomViewsArray) {
  648. CGFloat height = 0;
  649. for (UIView *view in self.sd_bottomViewsArray) {
  650. height = MAX(height, view.bottom);
  651. }
  652. contentHeight = height + self.sd_bottomViewBottomMargin;
  653. }
  654. if (self.sd_rightViewsArray) {
  655. CGFloat width = 0;
  656. for (UIView *view in self.sd_rightViewsArray) {
  657. width = MAX(width, view.right);
  658. }
  659. contentWidth = width + self.sd_rightViewRightMargin;
  660. }
  661. if ([self isKindOfClass:[UIScrollView class]]) {
  662. UIScrollView *scrollView = (UIScrollView *)self;
  663. CGSize contentSize = scrollView.contentSize;
  664. if (contentHeight > 0) {
  665. contentSize.height = contentHeight;
  666. }
  667. if (contentWidth > 0) {
  668. contentSize.width = contentWidth;
  669. }
  670. if (contentSize.width <= 0) {
  671. contentSize.width = scrollView.width;
  672. }
  673. if (!CGSizeEqualToSize(contentSize, scrollView.contentSize)) {
  674. scrollView.contentSize = contentSize;
  675. }
  676. } else {
  677. // 如果这里出现循环调用情况请把demo发送到gsdios@126.com,谢谢配合。
  678. if (self.sd_bottomViewsArray.count && (floorf(contentHeight) != floorf(self.height))) {
  679. self.height = contentHeight;
  680. self.fixedHeight = @(self.height);
  681. }
  682. if (self.sd_rightViewsArray.count && (floorf(contentWidth) != floorf(self.width))) {
  683. self.width = contentWidth;
  684. self.fixedWith = @(self.width);
  685. }
  686. }
  687. if (self.sd_rightViewsArray.count && (self.ownLayoutModel.right || self.ownLayoutModel.equalRight)) {
  688. SDAutoLayoutModel *model = self.ownLayoutModel;
  689. UIView *view = self;
  690. if (model.right) {
  691. if (view.superview == model.right.refView) {
  692. if (!view.fixedWith) { // view.autoLeft && view.autoRight
  693. view.width = model.right.refView.width - view.left - [model.right.value floatValue];
  694. }
  695. view.right = model.right.refView.width - [model.right.value floatValue];
  696. } else {
  697. if (!view.fixedWith) { // view.autoLeft && view.autoRight
  698. view.width = model.right.refView.left - view.left - [model.right.value floatValue];
  699. }
  700. view.right = model.right.refView.left - [model.right.value floatValue];
  701. }
  702. } else if (model.equalRight) {
  703. if (!view.fixedWith) {
  704. if (model.equalRight.refView == view.superview) {
  705. view.width = model.equalRight.refView.width - view.left;
  706. } else {
  707. view.width = model.equalRight.refView.right - view.left;
  708. }
  709. }
  710. view.right = model.equalRight.refView.right;
  711. if (view.superview == model.equalRight.refView) {
  712. view.right = model.equalRight.refView.width;
  713. }
  714. }
  715. }
  716. if (self.didFinishAutoLayoutBlock) {
  717. self.didFinishAutoLayoutBlock(self.frame);
  718. }
  719. }
  720. }
  721. - (void)sd_resizeWithModel:(SDAutoLayoutModel *)model
  722. {
  723. UIView *view = model.needsAutoResizeView;
  724. if (!view) return;
  725. if (view.sd_maxWidth && (model.rightSpaceToView || model.rightEqualToView)) { // 靠右布局前提设置
  726. if ([view isKindOfClass:[UILabel class]]) {
  727. UILabel *label = (UILabel *)view;
  728. CGFloat width = [view.sd_maxWidth floatValue] > 0 ? [view.sd_maxWidth floatValue] : MAXFLOAT;
  729. label.numberOfLines = 1;
  730. if (label.text.length) {
  731. if (!label.isAttributedContent) {
  732. CGRect rect = [label.text boundingRectWithSize:CGSizeMake(width, label.height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : label.font} context:nil];
  733. label.width = rect.size.width;
  734. } else{
  735. [label sizeToFit];
  736. if (label.width > width) {
  737. label.width = width;
  738. }
  739. }
  740. label.fixedWith = @(label.width);
  741. } else {
  742. label.width = 0;
  743. }
  744. }
  745. }
  746. if (model.width) {
  747. view.width = [model.width.value floatValue];
  748. view.fixedWith = @(view.width);
  749. } else if (model.ratio_width) {
  750. view.width = model.ratio_width.refView.width * [model.ratio_width.value floatValue];
  751. view.fixedWith = @(view.width);
  752. }
  753. if (model.height) {
  754. view.height = [model.height.value floatValue];
  755. view.fixedHeight = @(view.height);
  756. } else if (model.ratio_height) {
  757. view.height = [model.ratio_height.value floatValue] * model.ratio_height.refView.height;
  758. view.fixedHeight = @(view.height);
  759. }
  760. if (model.left) {
  761. if (view.superview == model.left.refView) {
  762. if (!view.fixedWith) { // view.autoLeft && view.autoRight
  763. view.width = view.right - [model.left.value floatValue];
  764. }
  765. view.left = [model.left.value floatValue];
  766. } else {
  767. if (!view.fixedWith) { // view.autoLeft && view.autoRight
  768. view.width = view.right - model.left.refView.right - [model.left.value floatValue];
  769. }
  770. view.left = model.left.refView.right + [model.left.value floatValue];
  771. }
  772. } else if (model.equalLeft) {
  773. if (!view.fixedWith) {
  774. view.width = view.right - model.equalLeft.refView.left;
  775. }
  776. if (view.superview == model.equalLeft.refView) {
  777. view.left = 0;
  778. } else {
  779. view.left = model.equalLeft.refView.left;
  780. }
  781. } else if (model.equalCenterX) {
  782. if (view.superview == model.equalCenterX.refView) {
  783. view.centerX = model.equalCenterX.refView.width * 0.5;
  784. } else {
  785. view.centerX = model.equalCenterX.refView.centerX;
  786. }
  787. } else if (model.centerX) {
  788. view.centerX = [model.centerX floatValue];
  789. }
  790. if (model.right) {
  791. if (view.superview == model.right.refView) {
  792. if (!view.fixedWith) { // view.autoLeft && view.autoRight
  793. view.width = model.right.refView.width - view.left - [model.right.value floatValue];
  794. }
  795. view.right = model.right.refView.width - [model.right.value floatValue];
  796. } else {
  797. if (!view.fixedWith) { // view.autoLeft && view.autoRight
  798. view.width = model.right.refView.left - view.left - [model.right.value floatValue];
  799. }
  800. view.right = model.right.refView.left - [model.right.value floatValue];
  801. }
  802. } else if (model.equalRight) {
  803. if (!view.fixedWith) {
  804. if (model.equalRight.refView == view.superview) {
  805. view.width = model.equalRight.refView.width - view.left;
  806. } else {
  807. view.width = model.equalRight.refView.right - view.left;
  808. }
  809. }
  810. view.right = model.equalRight.refView.right;
  811. if (view.superview == model.equalRight.refView) {
  812. view.right = model.equalRight.refView.width;
  813. }
  814. }
  815. if (view.autoHeightRatioValue && view.width > 0 && (model.bottomEqualToView || model.bottomSpaceToView)) { // 底部布局前提设置
  816. if ([view.autoHeightRatioValue floatValue] > 0) {
  817. view.height = view.width * [view.autoHeightRatioValue floatValue];
  818. } else {
  819. if ([view isKindOfClass:[UILabel class]]) {
  820. UILabel *label = (UILabel *)view;
  821. if (model.top || model.equalTop) {
  822. model.bottom = nil;
  823. model.equalBottom = nil;
  824. }
  825. label.numberOfLines = 0;
  826. if (label.text.length) {
  827. if (!label.isAttributedContent) {
  828. CGRect rect = [label.text boundingRectWithSize:CGSizeMake(label.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : label.font} context:nil];
  829. label.height = rect.size.height;
  830. } else {
  831. [label sizeToFit];
  832. }
  833. label.fixedHeight = @(label.height);
  834. } else {
  835. label.height = 0;
  836. }
  837. } else {
  838. view.height = 0;
  839. }
  840. }
  841. }
  842. if (model.top) {
  843. if (view.superview == model.top.refView) {
  844. if (!view.fixedHeight) { // view.autoTop && view.autoBottom && view.bottom
  845. view.height = view.bottom - [model.top.value floatValue];
  846. }
  847. view.top = [model.top.value floatValue];
  848. } else {
  849. if (!view.fixedHeight) { // view.autoTop && view.autoBottom && view.bottom
  850. view.height = view.bottom - model.top.refView.bottom - [model.top.value floatValue];
  851. }
  852. view.top = model.top.refView.bottom + [model.top.value floatValue];
  853. }
  854. } else if (model.equalTop) {
  855. if (view.superview == model.equalTop.refView) {
  856. if (!view.fixedHeight) {
  857. view.height = view.bottom;
  858. }
  859. view.top = 0;
  860. } else {
  861. if (!view.fixedHeight) {
  862. view.height = view.bottom - model.equalTop.refView.top;
  863. }
  864. view.top = model.equalTop.refView.top;
  865. }
  866. } else if (model.equalCenterY) {
  867. if (view.superview == model.equalCenterY.refView) {
  868. view.centerY = model.equalCenterY.refView.height * 0.5;
  869. } else {
  870. view.centerY = model.equalCenterY.refView.centerY;
  871. }
  872. } else if (model.centerY) {
  873. view.centerY = [model.centerY floatValue];
  874. }
  875. if (model.bottom) {
  876. if (view.superview == model.bottom.refView) {
  877. if (!view.fixedHeight) {
  878. view.height = view.superview.height - view.top - [model.bottom.value floatValue];
  879. }
  880. view.bottom = model.bottom.refView.height - [model.bottom.value floatValue];
  881. } else {
  882. if (!view.fixedHeight) {
  883. view.height = model.bottom.refView.top - view.top - [model.bottom.value floatValue];
  884. }
  885. view.bottom = model.bottom.refView.top - [model.bottom.value floatValue];
  886. }
  887. } else if (model.equalBottom) {
  888. if (view.superview == model.equalBottom.refView) {
  889. if (!view.fixedHeight) {
  890. view.height = view.superview.height - view.top;
  891. }
  892. view.bottom = model.equalBottom.refView.height;
  893. } else {
  894. if (!view.fixedHeight) {
  895. view.height = model.equalBottom.refView.bottom - view.top;
  896. }
  897. view.bottom = model.equalBottom.refView.bottom;
  898. }
  899. }
  900. if (view.sd_maxWidth) {
  901. if ([view isKindOfClass:[UILabel class]]) {
  902. UILabel *label = (UILabel *)view;
  903. CGFloat width = [view.sd_maxWidth floatValue] > 0 ? [view.sd_maxWidth floatValue] : MAXFLOAT;
  904. label.numberOfLines = 1;
  905. if (label.text.length) {
  906. if (!label.isAttributedContent) {
  907. CGRect rect = [label.text boundingRectWithSize:CGSizeMake(width, label.height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : label.font} context:nil];
  908. label.width = rect.size.width;
  909. label.fixedWith = @(label.width);
  910. } else{
  911. [label sizeToFit];
  912. if (label.width > width) {
  913. label.width = width;
  914. }
  915. }
  916. } else {
  917. label.width = 0;
  918. }
  919. }
  920. }
  921. if (model.maxWidth && [model.maxWidth floatValue] < view.width) {
  922. view.width = [model.maxWidth floatValue];
  923. }
  924. if (model.minWidth && [model.minWidth floatValue] > view.width) {
  925. view.width = [model.minWidth floatValue];
  926. }
  927. if (view.autoHeightRatioValue && view.width > 0) {
  928. if ([view.autoHeightRatioValue floatValue] > 0) {
  929. view.height = view.width * [view.autoHeightRatioValue floatValue];
  930. } else {
  931. if ([view isKindOfClass:[UILabel class]]) {
  932. UILabel *label = (UILabel *)view;
  933. label.numberOfLines = 0;
  934. if (label.text.length) {
  935. if (!label.isAttributedContent) {
  936. CGRect rect = [label.text boundingRectWithSize:CGSizeMake(label.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : label.font} context:nil];
  937. label.height = rect.size.height;
  938. } else {
  939. [label sizeToFit];
  940. }
  941. } else {
  942. label.height = 0;
  943. }
  944. } else {
  945. view.height = 0;
  946. }
  947. }
  948. }
  949. if (model.maxHeight && [model.maxHeight floatValue] < view.height) {
  950. view.height = [model.maxHeight floatValue];
  951. }
  952. if (model.minHeight && [model.minHeight floatValue] > view.height) {
  953. view.height = [model.minHeight floatValue];
  954. }
  955. if (model.widthEqualHeight) {
  956. view.width = view.height;
  957. }
  958. if (model.heightEqualWidth) {
  959. view.height = view.width;
  960. }
  961. if (view.didFinishAutoLayoutBlock) {
  962. view.didFinishAutoLayoutBlock(view.frame);
  963. }
  964. if (view.sd_bottomViewsArray.count || view.sd_rightViewsArray.count) {
  965. [view layoutSubviews];
  966. }
  967. CGFloat cornerRadius = view.layer.cornerRadius;
  968. CGFloat newCornerRadius = 0;
  969. if (view.sd_cornerRadius && (cornerRadius != [view.sd_cornerRadius floatValue])) {
  970. newCornerRadius = [view.sd_cornerRadius floatValue];
  971. } else if (view.sd_cornerRadiusFromWidthRatio && (cornerRadius != [view.sd_cornerRadiusFromWidthRatio floatValue] * view.width)) {
  972. newCornerRadius = view.width * [view.sd_cornerRadiusFromWidthRatio floatValue];
  973. } else if (view.sd_cornerRadiusFromHeightRatio && (cornerRadius != view.height * [view.sd_cornerRadiusFromHeightRatio floatValue])) {
  974. newCornerRadius = view.height * [view.sd_cornerRadiusFromHeightRatio floatValue];
  975. }
  976. if (newCornerRadius > 0) {
  977. view.layer.cornerRadius = newCornerRadius;
  978. view.clipsToBounds = YES;
  979. }
  980. }
  981. - (void)addAutoLayoutModel:(SDAutoLayoutModel *)model
  982. {
  983. [self.autoLayoutModelsArray addObject:model];
  984. }
  985. @end
  986. @implementation UIView (SDChangeFrame)
  987. - (CGFloat)left {
  988. return self.frame.origin.x;
  989. }
  990. - (void)setLeft:(CGFloat)x {
  991. CGRect frame = self.frame;
  992. frame.origin.x = x;
  993. self.frame = frame;
  994. }
  995. - (CGFloat)top {
  996. return self.frame.origin.y;
  997. }
  998. - (void)setTop:(CGFloat)y {
  999. CGRect frame = self.frame;
  1000. frame.origin.y = y;
  1001. self.frame = frame;
  1002. }
  1003. - (CGFloat)right {
  1004. return self.frame.origin.x + self.frame.size.width;
  1005. }
  1006. - (void)setRight:(CGFloat)right {
  1007. CGRect frame = self.frame;
  1008. frame.origin.x = right - frame.size.width;
  1009. self.frame = frame;
  1010. }
  1011. - (CGFloat)bottom {
  1012. return self.frame.origin.y + self.frame.size.height;
  1013. }
  1014. - (void)setBottom:(CGFloat)bottom {
  1015. CGRect frame = self.frame;
  1016. frame.origin.y = bottom - frame.size.height;
  1017. self.frame = frame;
  1018. }
  1019. - (CGFloat)centerX
  1020. {
  1021. return self.left + self.width * 0.5;
  1022. }
  1023. - (void)setCenterX:(CGFloat)centerX
  1024. {
  1025. self.left = centerX - self.width * 0.5;
  1026. }
  1027. - (CGFloat)centerY
  1028. {
  1029. return self.top + self.height * 0.5;
  1030. }
  1031. - (void)setCenterY:(CGFloat)centerY
  1032. {
  1033. self.top = centerY - self.height * 0.5;
  1034. }
  1035. - (CGFloat)width {
  1036. return self.frame.size.width;
  1037. }
  1038. - (void)setWidth:(CGFloat)width {
  1039. if (self.ownLayoutModel.widthEqualHeight) {
  1040. if (width != self.height) return;
  1041. }
  1042. CGRect frame = self.frame;
  1043. frame.size.width = width;
  1044. if (self.ownLayoutModel.heightEqualWidth) {
  1045. frame.size.height = width;
  1046. }
  1047. self.frame = frame;
  1048. }
  1049. - (CGFloat)height {
  1050. return self.frame.size.height;
  1051. }
  1052. - (void)setHeight:(CGFloat)height {
  1053. if (self.ownLayoutModel.heightEqualWidth) {
  1054. if (height != self.width) return;
  1055. }
  1056. CGRect frame = self.frame;
  1057. frame.size.height = height;
  1058. if (self.ownLayoutModel.widthEqualHeight) {
  1059. frame.size.width = height;
  1060. }
  1061. self.frame = frame;
  1062. }
  1063. - (CGPoint)origin {
  1064. return self.frame.origin;
  1065. }
  1066. - (void)setOrigin:(CGPoint)origin {
  1067. CGRect frame = self.frame;
  1068. frame.origin = origin;
  1069. self.frame = frame;
  1070. }
  1071. - (CGSize)size {
  1072. return self.frame.size;
  1073. }
  1074. - (void)setSize:(CGSize)size {
  1075. CGRect frame = self.frame;
  1076. frame.size = size;
  1077. self.frame = frame;
  1078. }
  1079. @end
  1080. @implementation SDUIViewCategoryManager
  1081. @end
  1082. // 版权属于原作者
  1083. // http://code4app.com (cn) http://code4app.net (en)
  1084. // 发布代码于最专业的源码分享网站: Code4App.com