iOS addSubViewのときに隅とか真ん中に楽に置けるカテゴリ
ただまんま書いただけなのですが、作っておくととても便利でした。
UIView+adjustSubView.h
#import <UIKit/UIKit.h> typedef enum { UIViewAdjustSubViewLeftTop, UIViewAdjustSubViewLeftCenter, UIViewAdjustSubViewLeftButtom, UIViewAdjustSubViewCenterTop, UIViewAdjustSubViewCenter, UIViewAdjustSubViewCenterButtom, UIViewAdjustSubViewRightTop, UIViewAdjustSubViewRightCenter, UIViewAdjustSubViewRightButtom, } UIViewAdjustSubView; @interface UIView (adjustSubView) - (void)addSubview:(UIView *)view location:(UIViewAdjustSubView)location margin:(CGFloat)margin; - (void)addSubview:(UIView *)view location:(UIViewAdjustSubView)location marginX:(CGFloat)marginX marginY:(CGFloat)marginY; @end
UIView+adjustSubView.m
#import "UIView+adjustSubView.h" @implementation UIView (adjustSubView) - (void)addSubview:(UIView *)view location:(UIViewAdjustSubView)location margin:(CGFloat)margin { [self addSubview:view location:location marginX:margin marginY:margin]; } - (void)addSubview:(UIView *)view location:(UIViewAdjustSubView)location marginX:(CGFloat)marginX marginY:(CGFloat)marginY { CGFloat x, y; switch (location) { case UIViewAdjustSubViewLeftTop: x = marginX; y = marginY; break; case UIViewAdjustSubViewLeftCenter: x = marginX; y = ( self.frame.size.height - view.frame.size.height) / 2; break; case UIViewAdjustSubViewLeftButtom: x = marginX; y = ( self.frame.size.height - view.frame.size.height - marginY ); break; case UIViewAdjustSubViewCenterTop: x = ( self.frame.size.width - view.frame.size.width) / 2; y = marginY; break; case UIViewAdjustSubViewCenter: x = ( self.frame.size.width - view.frame.size.width) / 2; y = ( self.frame.size.height - view.frame.size.height) / 2; break; case UIViewAdjustSubViewCenterButtom: x = ( self.frame.size.width - view.frame.size.width) / 2; y = ( self.frame.size.height - view.frame.size.height - marginY ); break; case UIViewAdjustSubViewRightTop: x = ( self.frame.size.width - view.frame.size.width - marginX ); y = marginY; break; case UIViewAdjustSubViewRightCenter: x = ( self.frame.size.width - view.frame.size.width - marginX ); y = ( self.frame.size.height - view.frame.size.height ) / 2; break; case UIViewAdjustSubViewRightButtom: x = ( self.frame.size.width - view.frame.size.width - marginX ); y = ( self.frame.size.height - view.frame.size.height - marginY ); break; default: break; } view.frame = CGRectMake(x, y, view.frame.size.width, view.frame.size.height); [self addSubview:view]; } @end
使い方
(右に置くときは縦横の間隔40px指定、それ以外は縦60px横20px指定)
// サイズが40pxの正方形のsubViewを作る UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; view1.backgroundColor = [UIColor blueColor]; UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; view2.backgroundColor = [UIColor blueColor]; // 省略... [self.view addSubview:view1 location:UIViewAdjustSubViewCenter marginX:20 marginY:60]; [self.view addSubview:view2 location:UIViewAdjustSubViewCenterButtom marginX:20 marginY:60]; [self.view addSubview:view3 location:UIViewAdjustSubViewCenterTop marginX:20 marginY:60]; [self.view addSubview:view4 location:UIViewAdjustSubViewLeftButtom marginX:20 marginY:60]; [self.view addSubview:view5 location:UIViewAdjustSubViewLeftCenter marginX:20 marginY:60]; [self.view addSubview:view6 location:UIViewAdjustSubViewLeftTop marginX:20 marginY:60]; [self.view addSubview:view7 location:UIViewAdjustSubViewRightButtom margin:40]; [self.view addSubview:view8 location:UIViewAdjustSubViewRightCenter margin:40]; [self.view addSubview:view9 location:UIViewAdjustSubViewRightTop margin:40];
単純ですが結構使ってます。
こういう自分パーツをいっぱい作っておけばどんどん効率化できそうですね。
あと、QuickDialogのカスタマイズとかできるようになればかなり捗りそうなんで、今の課題ですね。