1:Delegate运用
.h #import <UIKit/UIKit.h> @protocol FilterHeaderViewDelegate <NSObject> @required -(void)filterHeaderViewMoreBtnClicked:(id)sender; @end extern float CYLFilterHeaderViewHeigt; @interface FilterHeaderView : UICollectionReusableView @property (nonatomic, weak ) id<FilterHeaderViewDelegate> delegate; @end .m - (void)moreBtnClicked:(id)sender { if ([self.delegate respondsToSelector:@selector(filterHeaderViewMoreBtnClicked:)]) { [self.delegate filterHeaderViewMoreBtnClicked:self.moreButton]; } } 注意在.m中会有要调用上面这个方法: [self.moreButton addTarget:self action:@selector(moreBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; 而在调用这个插件的时候记得把delegate赋于self,并要把FilterHeaderViewDelegate引入到<>里;然后就可以实现这个deletage的方法;
2:实现UIButton不同状态下的显示
[btn setTitle:@"更多" forState:UIControlStateNormal]; [btn setTitle:@"收起" forState:UIControlStateSelected]; btn.titleLabel.font = [UIFont systemFontOfSize:12]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; btn.adjustsImageWhenHighlighted = NO; [btn setImage:[UIImage imageNamed:@"home_btn_more_normal"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"home_btn_more_selected"] forState:UIControlStateSelected]; btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width-kImageToTextMargin, 0, btn.imageView.frame.size.width); btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width, 0, -btn.titleLabel.frame.size.width);
3:视图uiview增加点击事件
if (!_TicketView) { _TicketView=[UIView new]; _TicketView.backgroundColor=[UIColor whiteColor]; //增加点击事件 UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TickAction:)]; [_TicketView addGestureRecognizer:tapGesture]; [_BelowView addSubview:_TicketView]; } 调用: -(void)TickAction:(id)sender { NSLog(@"sdfsdfsdf"); }
4:在ios7中出现滚动视图UIScrollView不兼容问题,无法滚动
把contentSize定义放在viewDidLayoutSubviews中; -(void)viewDidLayoutSubviews { _myScrollView.contentSize=CGSizeMake(SCREEN_WIDTH,600); }
5:对UIButton上的图标进行翻转
#define DEGREES_TO_RADIANS(angle) ((angle)/180.0 *M_PI) 调用: _BtnMoreContent.imageView.transform = CGAffineTransformRotate(_BtnMoreContent.imageView.transform, DEGREES_TO_RADIANS(180));
6:创建一个背景图片,并从网络动态加载
在viewDidLoad调用: -(void)LoadBackViewImage { UIImageView *bgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; bgView.image=[UIImage imageNamed:@"bgWeatherEmpty"]; [bgView sd_setImageWithURL:[NSURL URLWithString:self.backImageUrl] placeholderImage:[UIImage imageNamed:@"bgWeatherEmpty"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { dispatch_async(dispatch_get_main_queue(), ^{ bgView.image=image; [self.view addSubview:bgView]; [self.view sendSubviewToBack:bgView]; }); }]; } 注意: UIView层次管理(sendSubviewToBack,bringSubviewToFront) 将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront()方法。 将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack()方法。
7:加载xib到其它视图中
创建一个UIView的xib文件,这个方程可以看网上随便一个文章,主要是把属性中的custom Class指定给创建的那个 .h文件; .h #import <UIKit/UIKit.h> @interface LKTextView : UIView @property (strong, nonatomic) IBOutlet UILabel *lbText; - (IBAction)bt_pressed:(id)sender; @property (strong, nonatomic) IBOutlet UITextField *textView; +(LKTextView*)instanceTextView; @end .m #import "LKTextView.h" #import "RKTabView.h" #import "RKTabItem.h" @implementation LKTextView @synthesize textView; @synthesize lbText; +(LKTextView *)instanceTextView { NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"LKTextView" owner:nil options:nil]; return [nibView objectAtIndex:0]; } -(id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if(self) { //其它自个想增加到视图里的 [self initViews]; } return self; } /** * @author wujunyang, 15-04-30 16:04:03 * * @brief 这边是运用RKTABVIEW创建一个标签的 */ -(void)initViews { UIView *vi=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)]; vi.backgroundColor=[UIColor redColor]; [self addSubview:vi]; RKTabView *titledTabsView=[[RKTabView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)]; [self addSubview:titledTabsView]; RKTabItem *mastercardTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"mastercard"]]; mastercardTabItem.titleString = @"MasterCard"; RKTabItem *paypalTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"paypal"]]; paypalTabItem.titleString = @"PayPal"; RKTabItem *visaTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"visa"]]; visaTabItem.titleString = @"Visa"; RKTabItem *wuTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"wu"]]; wuTabItem.titleString = @"Western Union"; RKTabItem *wireTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"wire-transfer"]]; wireTabItem.titleString = @"Wire Transfer"; //mastercardTabItem.tabState = TabStateEnabled; titledTabsView.darkensBackgroundForEnabledTabs = YES; titledTabsView.horizontalInsets = HorizontalEdgeInsetsMake(25, 25); titledTabsView.titlesFontColor = [UIColor colorWithWhite:0.9f alpha:0.8f]; titledTabsView.tabItems = @[mastercardTabItem, paypalTabItem, visaTabItem, wuTabItem, wireTabItem]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (IBAction)bt_pressed:(id)sender { lbText.text = textView.text; } @end 然后调用视图.m: - (void)viewDidLoad { LKTextView* text = [LKTextView instanceTextView]; text.frame = CGRectMake(100, 100, text.frame.size.width, text.frame.size.height); text.textView.text = @"input "; [self.view addSubview:text]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }
8:不错的插件
AutoLayout下自动计算UITableViewCell高度的扩展FDTemplateLayoutCell https://github.com/forkingdog/UITableView-FDTemplateLayoutCell 多视图控制器间进行切换--XLPagerTabStrip https://github.com/xmartlabs/XLPagerTabStrip 不错的标签选项卡插件 https://github.com/RafaelKayumov/RKTabView
9:ios7和ios8关于导航栏的那些事
ios7之前的版本中UIViewController中的view在显示后会自动调整为去掉导航栏的高度的,控件会自动在导航栏以下摆放。 在iOS7中UIViewController的wantsFullScreenLayout属性被舍弃了,所有的UIViewController创建后默认就是full Screen的,因此如果带导航栏的应用界面中的部分控件会被导航栏覆盖掉。 解决方案:可以使用ios7种UIViewController新增的属性extendLayoutIncludesOpaqueBars和edgesForExtendedLayout来解决。 其中这个属性指定了当bar使用不透明图片时,试图是否延伸至bar所在区域,默认值为NO。而edgesForExtendedLayout 其中这个属性指定了当Bar使用了不透明图片时,视图是否延伸至Bar所在区域,默认值时NO。而edgesForExtendedLayout则是表示视图是否覆盖到四周的区域,默认是UIRectEdgeAll,即上下左右四个方向都会覆盖,那么为让顶部不进行延伸到导航栏覆盖的区域,我们可以把顶部区域延伸去掉。实现代码如下: self.extendedLayoutIncludesOpaqueBars = NO; self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;