IOS UIView quick modification of frame, iosuiview

Source: Internet
Author: User

IOS UIView quick modification of frame, iosuiview

Complex code implementation is required when frame is modified in iOS development layout. Today, I occasionally see a podcast about the fast modification of frame method. I wrote the implementation code myself.

To achieve quick implementation, you can add some methods for directly modifying the frame attribute (for example, obtaining the height, width, and coordinates) to the UIView control by adding a category. The Code is as follows:

. H file, declare the attribute to be used

1 // 2 // UIView + Layout. h 3 // Layout 4 // 5 // Created by Ager on 15/10/18. 6 // Copyright©2015 Ager. all rights reserved. 7 // 8 9 # import <UIKit/UIKit. h> 10 11 @ interface UIView (Layout) 12 13 // top, bottom, left, right 14 @ property (nonatomic, assign) CGFloat top; 15 @ property (nonatomic, assign) CGFloat bottom; 16 @ property (nonatomic, assign) CGFloat left; 17 @ property (nonatomic, assign) CGFloat right; 18 19 // coordinate, x, y20 @ property (nonatomic, assign) CGFloat x; 21 @ property (nonatomic, assign) CGFloat y; 22 @ property (nonatomic, assign) CGPoint origin; 23 24 // Center Coordinate centerX, centerY25 @ property (nonatomic, assign) CGFloat centerX; 26 @ property (nonatomic, assign) CGFloat centerY; 27 28 29 // size, width, height 30 @ property (nonatomic, assign) CGFloat width; 31 @ property (nonatomic, assign) CGFloat height; 32 @ property (nonatomic, assign) CGSize size; 33 34 @ endView Code

The. m file implements operations on attributes to modify the frame.

1 // 2 // UIView + Layout. m 3 // Layout 4 // 5 // Created by Ager on 15/10/18. 6 // Copyright©2015 Ager. all rights reserved. 7 // 8 9 # import "UIView + Layout. h "10 11 @ implementation UIView (Layout) 12 13 // top, bottom, left, right 14 // top; 15-(CGFloat) top {16 return self. frame. origin. y; 17} 18 19-(void) setTop :( CGFloat) top {20 CGRect frame = self. frame; 21 frame. origin. y = top; 22 self. frame = frame; 23} 24 25 // bottom; 26-(CGFloat) bottom {27 return CGRectGetMaxY (self. frame); 28} 29 30-(void) setBottom :( CGFloat) bottom {31 CGRect frame = self. frame; 32 frame. origin. y = [self bottom]-[self height]; 33 self. frame = frame; 34} 35 36 // left; 37-(CGFloat) left {38 return self. frame. origin. x; 39} 40 41-(void) setLeft :( CGFloat) left {42 CGRect frame = self. frame; 43 frame. origin. x = left; 44 self. frame = frame; 45} 46 47 // right; 48-(CGFloat) right {49 return CGRectGetMaxX (self. frame); 50} 51 52-(void) setRight :( CGFloat) right {53 CGRect frame = self. frame; 54 frame. origin. x = [self right]-[self width]; 55 self. frame = frame; 56} 57 58 59 // coordinate, x, y 60 // x; 61-(CGFloat) x {62 return self. frame. origin. x; 63} 64 65-(void) setX :( CGFloat) x {66 CGRect frame = self. frame; 67 frame. origin. x = x; 68 self. frame = frame; 69} 70 71 // y; 72 73-(CGFloat) y {74 return self. origin. y; 75} 76 77-(void) setY :( CGFloat) y {78 CGRect frame = self. frame; 79 frame. origin. y = y; 80 self. frame = frame; 81} 82 83 // origin; 84-(CGPoint) origin {85 return self. frame. origin; 86} 87 88-(void) setOrigin :( CGPoint) origin {89 CGRect frame = self. frame; 90 self. origin = origin; 91 self. frame = frame; 92} 93 94 95 // Center Coordinate centerX, centerY 96 // centerX; 97-(CGFloat) centerX {98 return self. center. x; 99} 100 101-(void) setCenterX :( CGFloat) centerX {102 CGPoint center = self. center; 103 center. x = centerx; 104 self. center = center; 105} 106 107 108 // centerY; 109-(CGFloat) centerY {110 return self. center. y; 111} 112 113-(void) setCenterY :( CGFloat) centerY {114 CGPoint center = self. center; 115 center. y = centerY; 116 self. center = center; 117} 118 119 120 // size, 121 // width; 122-(CGFloat) width {123 return self. frame. size. width; 124} 125 126-(void) setWidth :( CGFloat) width {127 CGRect frame = self. frame; 128 frame. size. width = width; 129 self. frame = frame; 130} 131 132 133 // height; 134-(CGFloat) height {135 return self. frame. size. height; 136} 137 138-(void) setHeight :( CGFloat) height {139 CGRect frame = self. frame; 140 frame. size. height = height; 141 self. frame = frame; 142} 143 144 // size; 145-(CGSize) size {146 return self. frame. size; 147} 148 149 150-(void) setSize :( CGSize) size {151 CGRect frame = self. frame; 152 frame. size = size; 153 self. frame = frame; 154} 155 156 @ endView Code

Example:

1 // modify the width of 2 aview. width = 300; 3 // modify x coordinate 4 aview. x = 100; 5 // modify y coordinate 6 aview. y = 100;View Code

 

 

Original reference: http://mp.weixin.qq.com/s? _ Biz = MzA3NzM0NzkxMQ ==& mid = 216102953 & idx = 2 & sn = Hangzhou & scene = 23 & srcid = 1018RAYFkM4iK97OMvC1c2PP # rd

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.