iOS 随意设置button图片与文字的frame
2016-07-21 本文已影响0人
StevenF
交换方法的分类
#import <Foundation/Foundation.h>
@interface NSObject (SwizzleMethod)
+ (void)swizzleMethod:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector;
@end
#import "NSObject+SwizzleMethod.h"
@implementation NSObject (SwizzleMethod)
+ (void)swizzleMethod:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector; {
Method originalMethod = class_getInstanceMethod([self class], originalSelector);
Method swizzledMethod = class_getInstanceMethod([self class], swizzledSelector);
if (class_addMethod([self class],originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod))) {
class_replaceMethod([self class],swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@end
代码:创建UIButton (LayoutSubviews)
# import <UIKit/UIKit.h>
@interface UIButton (LayoutSubviews)
@property (assign, nonatomic) CGRect imageViewFrame;
@property (assign, nonatomic) CGRect titleLabelFrame;
@end
#import "UIButton+LayoutSubviews.h"
@implementation UIButton (LayoutSubviews)
+ (void)load; {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self swizzleMethod:@selector(layoutSubviews) swizzledSelector:@selector(aop_layoutSubviews)];
});
}
- (CGRect)imageViewFrame; {
return [objc_getAssociatedObject(self, @selector(imageViewFrame)) CGRectValue];
}
- (void)setImageViewFrame:(CGRect)imageViewFrame; {
objc_setAssociatedObject(self, @selector(imageViewFrame), [NSValue valueWithCGRect:imageViewFrame], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGRect)titleLabelFrame; {
return [objc_getAssociatedObject(self, @selector(titleLabelFrame)) CGRectValue];
}
- (void)setTitleLabelFrame:(CGRect)titleLabelFrame; {
objc_setAssociatedObject(self, @selector(titleLabelFrame), [NSValue valueWithCGRect:titleLabelFrame], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)aop_layoutSubviews; {
[self aop_layoutSubviews];
[CATransaction begin];
[CATransaction setDisableActions:YES];
CGRect imageRect = self.imageViewFrame;
if (!CGRectEqualToRect(imageRect, CGRectZero)) {
self.imageView.frame.origin.y, self.imageView.frame.size.width, self.imageView.frame.size.height);
if (!CGRectEqualToRect(self.imageView.frame, imageRect)) {
self.imageView.frame = imageRect;
}
}
if (!CGRectEqualToRect(self.titleLabelFrame, CGRectZero)) {
if (!CGRectEqualToRect(self.titleLabel.frame, self.titleLabelFrame)) {
self.titleLabel.frame = self.titleLabelFrame;
}
}
[CATransaction commit];
}
@end
在外部直接设置UIButton的titleLabel和imageView的位置就可以了。