判斷是否是第一次打開應(yīng)用程序
先獲取CFBundleShortVersionString這個(gè)key囊蓝,然后從沙盒中取這個(gè)key饿悬,如果兩個(gè)相等,則表示不是第一次打開該應(yīng)用程序聚霜,不相等的時(shí)候應(yīng)該把沙盒中中的key保存起來(lái)狡恬,下次啟動(dòng)時(shí)候再進(jìn)行判斷
NSString * key = @"CFBundleShortVersionString";
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
NSString *sandboxVersion = [[NSUserDefaults standardUserDefaults] stringForKey:key];
if (![currentVersion isEqualToString:sandboxVersion]) {
[[NSUserDefaults standardUserDefaults]setObject:currentVersion forKey:key];
}else{
}
###修改tabbariterm選中以及未選中顏色
我們可以調(diào)用UIBarItem的這個(gè)`- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;`方法,這個(gè)方法支持在appearance的時(shí)候設(shè)置蝎宇,為什么設(shè)置UIBarItem呢弟劲,因?yàn)閁ITabBarItem繼承自UIBarItem
```objc
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
替換系統(tǒng)自帶的tabbar
我們可以使用KVC把系統(tǒng)的tabbar替換成我們自己的
在TabBarController的viewDidLoad中進(jìn)行設(shè)置即可
[self setValue:[[RYFTabBar alloc]init] forKey:@"tabBar"];
自定義tabbar
現(xiàn)在很多APP中都要求中間有一個(gè)加號(hào)按鈕或者什么的,這個(gè)時(shí)候就需要我們自定義tabbar了姥芥,當(dāng)然網(wǎng)上有很多自定義tabbar的demo兔乞,不過(guò)這個(gè)方式實(shí)現(xiàn)比較簡(jiǎn)單,我們需要聲明一個(gè)協(xié)議,然后監(jiān)聽按鈕的點(diǎn)擊庸追,我們先在- (instancetype)initWithFrame:(CGRect)frame
中創(chuàng)建中間的按鈕霍骄,然后在layoutSubviews
中設(shè)置frame,因?yàn)榘砂粹o添加到tabbar上面的時(shí)候會(huì)觸發(fā)layoutSubviews
锚国,在這個(gè)方法里面拿到的frame比較準(zhǔn)確腕巡。代碼如下
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
_plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_plusButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[_plusButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
[_plusButton addTarget:self action:@selector(plusSignClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_plusButton];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
_plusButton.bounds = CGRectMake(0, 0, _plusButton.currentBackgroundImage.size.width, _plusButton.currentBackgroundImage.size.height);
_plusButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
NSInteger index = 0;
CGFloat buttonY = 0;
CGFloat buttonW = self.frame.size.width / 5;
CGFloat buttonH = self.frame.size.height;
for (UIView *view in self.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
CGFloat buttonX = buttonW * ((index > 1) ? (index + 1) : index);
view.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
index ++;
}
}
}
- (void)plusSignClick
{
if ([self.tabBarDelegate respondsToSelector:@selector(plusBtnClick)]) {
[self.tabBarDelegate plusBtnClick];
}
}
設(shè)置導(dǎo)航欄UIBarButtonItem顏色以及字體
/**
* 當(dāng)?shù)谝淮问褂眠@個(gè)類的時(shí)候會(huì)調(diào)用一次
*/
+ (void)initialize
{
UINavigationBar *navigationBar = [UINavigationBar appearance];
[navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
//更改title文字可以設(shè)置self.navigationItem.titleView也可以設(shè)置setTitleTextAttributes
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:15];
dict[NSForegroundColorAttributeName] = [UIColor blackColor];
[navigationBar setTitleTextAttributes:dict];//UI_APPEARANCE_SELECTOR設(shè)置一次就行了
//設(shè)置barbuttonitem屬性
UIBarButtonItem *item = [UIBarButtonItem appearance];
NSMutableDictionary *itemAttrs= [NSMutableDictionary dictionary];
itemAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
itemAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
[item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
// UIControlStateDisabled
NSMutableDictionary *itemDisabledAttrs = [NSMutableDictionary dictionary];
itemDisabledAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
[item setTitleTextAttributes:itemDisabledAttrs forState:UIControlStateDisabled];
}
設(shè)置導(dǎo)航欄title顏色以及字體
UINavigationBar *bar = self.navigationController.navigationBar;
[bar setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor whiteColor]}];
導(dǎo)航欄透明
設(shè)置UINavigationBar全透明, 此處隨便設(shè)置一張圖片即可血筑,重要的是BarMetrics屬性決定了bar的樣式
UINavigationBar *bar = self.navigationController.navigationBar绘沉;
[bar setBackgroundImage:[UIImage imageNamed:@"bg.png"] forBarMetrics:UIBarMetricsCompact];
push到下一個(gè)控制器的時(shí)候隱藏tabbar
這時(shí)候我們可以重寫- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
這個(gè)方法,當(dāng)然我們也可以在這個(gè)方法里面設(shè)置返回按鈕
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn setTitle:@"返回" forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
btn.size = CGSizeMake(70, 30);
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
解決重寫返回按鈕的時(shí)候可能會(huì)導(dǎo)致系統(tǒng)自帶的左側(cè)滑動(dòng)返回實(shí)現(xiàn)
可以使用孫源大神寫的那個(gè)FDFullscreenPopGesture
豺总,也可以設(shè)置self.interactivePopGestureRecognizer.delegate = nil;
车伞,在導(dǎo)航控制器中設(shè)置即可
自定義UIBarButtonItem
我們可以給UIBarButtonItem添加一個(gè)分類,這樣用起來(lái)也比較方便
+ (instancetype)itemWithimage:(NSString *)image heighlitImage:(NSString *)heighlitImage target:(id)target ation:(SEL)action
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:heighlitImage] forState:UIControlStateHighlighted];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
btn.size = btn.currentBackgroundImage.size;
return [[self alloc]initWithCustomView:btn];
}
狀態(tài)欄的設(shè)置
//iOS7以后狀態(tài)欄由controller進(jìn)行控制喻喳,如果不需要控制器控制的話需要在info.plist設(shè)置View controller-based status bar appearancer然后通過(guò)[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;設(shè)置
如果不想設(shè)置plist另玖,我們可以重寫- (UIStatusBarStyle)preferredStatusBarStyle
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
修改UITextField的placeholder顏色
- 修改Placeholder的方法有好幾種,可以設(shè)置textField的attributedPlaceholder屬性表伦,這是一個(gè)富文本屬性谦去,
- 也可以自定義一個(gè)TextField,重寫- (void)drawPlaceholderInRect:(CGRect)rect這個(gè)方法[self.placeholder drawInRect:rect withAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
- 就是通過(guò)Runtime運(yùn)行時(shí)進(jìn)行修改
- 使用屬性
@property(nonatomic,copy) NSAttributedString *attributedPlaceholder;
// 文字屬性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
// NSAttributedString : 帶有屬性的文字(富文本技術(shù))
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"手機(jī)號(hào)" attributes:attrs];
self.phoneField.attributedPlaceholder = placeholder;
NSMutableAttributedString *placehoder = [[NSMutableAttributedString alloc] initWithString:@"手機(jī)號(hào)"];
[placehoder setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} range:NSMakeRange(0, 1)];
[placehoder setAttributes:@{
NSForegroundColorAttributeName : [UIColor yellowColor],
NSFontAttributeName : [UIFont systemFontOfSize:30]
} range:NSMakeRange(1, 1)];
[placehoder setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(2, 1)];
self.phoneField.attributedPlaceholder = placehoder;
- 重寫方法
- (void)drawPlaceholderInRect:(CGRect)rect
{
[self.placeholder drawInRect:CGRectMake(0, 10, rect.size.width, 25) withAttributes:@{
NSForegroundColorAttributeName : [UIColor grayColor],
NSFontAttributeName : self.font}];
}
- 使用KVC
[self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
圖片在上面文字在下面的UIButton
我們可以自定義一個(gè)UIButton
我們?cè)?code>awakeFromNib以及initWithFrame
設(shè)置文字的居中蹦哼,在layoutSubviews
設(shè)置UIButton的圖片位置大小以及l(fā)able的位置大小
- (void)awakeFromNib
{
[self setup];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
- (void)setup
{
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.imageView.x = 0;
self.imageView.y = 0;
self.imageView.width = self.imageView.width;
self.imageView.height = self.imageView.width;
self.titleLabel.x = 0;
self.titleLabel.y = self.imageView.height + 10;
self.titleLabel.width = self.imageView.width;
self.titleLabel.height = self.titleLabel.height;
}
SDWebImage獲取緩存圖片大小
SDWebImage已經(jīng)提供了方法獲取圖片占據(jù)內(nèi)存大小了鳄哭,我們調(diào)用即可
[[SDImageCache sharedImageCache]getSize]
- 清空緩存
[[SDImageCache sharedImageCache]cleanDisk];
帶有placeholder的UITextView
一個(gè)好的自定義控件最好有提供一些其他屬性供其他人用,這里提供了一個(gè)設(shè)置placeholder顏色以及字體的屬性纲熏,使用懶加載先設(shè)置一下placeholder的基本屬性以及x妆丘,y以及設(shè)置默認(rèn)的顏色,然后在initWithFrame方法里面監(jiān)聽UITextViewTextDidChangeNotification
這個(gè)通知局劲,通過(guò)UITextView是否有文字輸入從而判斷placeholder是否隱藏勺拣,重寫setfont,settext以及setAttributedText方法鱼填,代碼如下
- (UILabel *)placeholerLabel
{
if (!_placeholerLabel) {
_placeholerLabel = [[UILabel alloc]init];
[self addSubview:_placeholerLabel];
_placeholerLabel.numberOfLines = 0;
_placeholerLabel.x = 5;
_placeholerLabel.y = 5;
_placeholerLabel.textColor = [UIColor lightGrayColor];
}
return _placeholerLabel;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.alwaysBounceVertical = YES;
self.font = [UIFont systemFontOfSize:15];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
}
- (void)textChange
{
self.placeholerLabel.hidden = self.hasText;
}
- (void)setPlaceholer:(NSString *)placeholer
{
_placeholer = placeholer;
self.placeholerLabel.text = placeholer;
[self setNeedsLayout];
}
- (void)setPlaceholerColor:(UIColor *)placeholerColor
{
_placeholerColor = placeholerColor;
self.placeholerLabel.textColor = placeholerColor;
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
self.placeholerLabel.font = font;
[self setNeedsLayout];
}
- (void)setText:(NSString *)text
{
[super setText:text];
[self textChange];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
[self textChange];
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.placeholerLabel.width = SCREENWIDTH - 2 * self.placeholerLabel.x;
[self.placeholerLabel sizeToFit];
}
/**
* 更新占位文字的尺寸
*/
- (void)updatePlaceholderLabelSize
{
CGSize maxSize = CGSizeMake(SCREENWIDTH - 2 * self.placeholerLabel.x, MAXFLOAT);
self.placeholerLabel.size = [self.placeholer boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil].size;
// self.placeholerLabel.backgroundColor = [UIColor redColor];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
另一種是使用drawRect方法實(shí)現(xiàn)的
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.alwaysBounceVertical = YES;
self.font = [UIFont systemFontOfSize:15];
self.placeholerColor = [UIColor lightGrayColor];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
}
- (void)textChange
{
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
//可以使用這種方式判斷
// if (self.text.length || self.attributedText.length) return;
//或者是這種
if (self.hasText) return;
rect.origin.x = 5;
rect.origin.y = 5;
rect.size.width -= 2 * rect.origin.x;
NSMutableDictionary *att = [NSMutableDictionary dictionary];
att[NSFontAttributeName] = self.font;
att[NSForegroundColorAttributeName] = self.placeholerColor;
[self.placeholer drawInRect:rect withAttributes:att];
}
- (void)setPlaceholer:(NSString *)placeholer
{
_placeholer = placeholer;
[self setNeedsDisplay];
}
- (void)setPlaceholerColor:(UIColor *)placeholerColor
{
_placeholerColor = placeholerColor;
[self setNeedsDisplay];
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
[self setNeedsDisplay];
}
- (void)setText:(NSString *)text
{
[super setText:text];
[self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
[self setNeedsDisplay];
}
當(dāng)點(diǎn)擊一個(gè)按鈕就發(fā)送一次請(qǐng)求的時(shí)候药有,如果不想連著點(diǎn)兩下發(fā)兩次網(wǎng)絡(luò)請(qǐng)求,這時(shí)候我們可以把請(qǐng)求參數(shù)弄一個(gè)property苹丸,然后比較兩次是否相同塑猖,相同就不發(fā)請(qǐng)求
當(dāng)網(wǎng)絡(luò)比較慢的時(shí)候,我們從當(dāng)前頁(yè)面返回到了上一個(gè)頁(yè)面谈跛,這時(shí)候可能后來(lái)返回?cái)?shù)據(jù)的時(shí)候造成崩潰,我們可以重寫dealloc方法塑陵,在dealloc方法中把這次請(qǐng)求給取消掉
cell選中顏色
//highlightedTextColor可以設(shè)置選中時(shí)候字體的顏色
self.textLabel.highlightedTextColor = RGBCOLOR(215, 27, 26);
改變cell的大小
我們可以在layoutSubviews中設(shè)置
- (void)layoutSubviews
{
[super layoutSubviews];
//如果想改變系統(tǒng)中某一個(gè)控件的位置可以在layoutSubviews重新設(shè)置控件的位置
self.textLabel.height = self.height - 2;
}
cell選擇一個(gè)標(biāo)記視圖
我們可以在- (void)setSelected:(BOOL)selected animated:(BOOL)animated 這個(gè)方法中設(shè)置
當(dāng)選中cell的時(shí)候如果設(shè)置了selectionStyle為none的話會(huì)造成self.textLabel.highlightedTextColor這個(gè)顏色無(wú)法正常顯示感憾,解決這個(gè)問題的方法就是在- (void)setSelected:(BOOL)selected animated:(BOOL)animated這個(gè)方法中設(shè)置選中時(shí)字體顏色以及普通狀態(tài)下的文字顏色,cell被選中時(shí)內(nèi)部子控件不會(huì)進(jìn)入高亮狀態(tài)
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
/**
* 當(dāng)選中cell的時(shí)候如果設(shè)置了selectionStyle為none的話會(huì)造成self.textLabel.highlightedTextColor這個(gè)顏色無(wú)法正常顯示,解決這個(gè)問題的方法就是在- (void)setSelected:(BOOL)selected animated:(BOOL)animated這個(gè)方法中設(shè)置選中時(shí)字體顏色以及普通狀態(tài)下的文字顏色,cell被選中時(shí)內(nèi)部子控件不會(huì)進(jìn)入高亮狀態(tài)
*
*
*/
_selectView.hidden = !selected;
self.textLabel.textColor = !selected ? RGBCOLOR(74, 74, 74) : RGBCOLOR(215, 27, 26);
}
改變UITextField提示文字顏色
static NSString * const PlacerholderColorKeyPath = @"_placeholderLabel.textColor";
- (void)drawPlaceholderInRect:(CGRect)rect
{
[super drawPlaceholderInRect:rect];
//需要設(shè)置位置
[self.placeholder drawInRect:CGRectMake(0, 5, 0, 0) withAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)awakeFromNib
{
//設(shè)置光標(biāo)顏色和文字顏色一樣
self.tintColor = self.textColor;
[self resignFirstResponder];
}
- (BOOL)becomeFirstResponder
{
[self setValue:[UIColor whiteColor] forKeyPath:PlacerholderColorKeyPath];
return [super becomeFirstResponder];
}
- (BOOL)resignFirstResponder
{
[self setValue:[UIColor grayColor] forKeyPath:PlacerholderColorKeyPath];
return [super resignFirstResponder];
}
判斷一個(gè)控件是否在主窗口上面
首先把當(dāng)前控件坐標(biāo)轉(zhuǎn)換成其父控件所在坐標(biāo)系的rect,然后判斷轉(zhuǎn)換后的rect是否在主窗口的bounds之內(nèi)阻桅,然后只有當(dāng)前控件顯示以及透明度大于0.01 以及當(dāng)前控件所在的窗口為主窗口以及轉(zhuǎn)換后的rect是否在主窗口的bounds之內(nèi)凉倚,只有這些條件都成立的時(shí)候這個(gè)控件才在主窗口上面顯示,代碼如下
- (BOOL)isShowOnWindow
{
//主窗口
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
//相對(duì)于父控件轉(zhuǎn)換之后的rect
CGRect newRect = [keyWindow convertRect:self.frame fromView:self.superview];
//主窗口的bounds
CGRect winBounds = keyWindow.bounds;
//判斷兩個(gè)坐標(biāo)系是否有交匯的地方嫂沉,返回bool值
BOOL isIntersects = CGRectIntersectsRect(newRect, winBounds);
if (self.hidden != YES && self.alpha >0.01 && self.window == keyWindow && isIntersects) {
return YES;
}else{
return NO;
}
}
在XIB或者SB中設(shè)置控件圓角或者邊框
我們可以寫一個(gè)UIView的分類稽寒,然后在分類中加入IB_DESIGNABLE,以及property(borderWidth,borderColor,cornerRadius)這些property前面需要使用IBInspectable修飾才會(huì)有效果趟章,然后重寫set方法即可
IB_DESIGNABLE
@interface UIView (RYF)
@property (nonatomic, assign)CGFloat x;
@property (nonatomic, assign)CGFloat y;
@property (nonatomic, assign)CGFloat width;
@property (nonatomic, assign)CGFloat height;
@property (nonatomic, assign)CGFloat centerX;
@property (nonatomic, assign)CGFloat centerY;
@property (nonatomic, assign)CGSize size;
@property(nonatomic, assign) IBInspectable CGFloat borderWidth;
@property(nonatomic, assign) IBInspectable UIColor *borderColor;
@property(nonatomic, assign) IBInspectable CGFloat cornerRadius;
- (void)setBorderWidth:(CGFloat)borderWidth
{
if (borderWidth < 0) {
return;
}
self.layer.borderWidth = borderWidth;
}
- (void)setBorderColor:(UIColor *)borderColor
{
self.layer.borderColor = borderColor.CGColor;
}
- (void)setCornerRadius:(CGFloat)cornerRadius
{
self.layer.cornerRadius = cornerRadius;
self.layer.masksToBounds = YES;
}
判斷當(dāng)前view所屬的控制器
UIView的分類里面寫的一個(gè)對(duì)象方法
- (UIViewController *)parentController
{
UIResponder *responder = [self nextResponder];
while (responder) {
if ([responder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)responder;
}
responder = [responder nextResponder];
}
return nil;
}
當(dāng)設(shè)置某一個(gè)控件為tableview的tableHeaderView時(shí)候杏糙,當(dāng)滑動(dòng)tableview的時(shí)候會(huì)一直調(diào)用找個(gè)控件的setframe方法
連續(xù)點(diǎn)擊tabbar刷新tableView或者返回至最上面
首先我們現(xiàn)在AppDelegate當(dāng)中監(jiān)聽tabbar的點(diǎn)擊,通過(guò)- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
,然后發(fā)出通知蚓土,在需要的地方監(jiān)聽宏侍,然后判斷當(dāng)前view是否在主窗口上面以及設(shè)置一個(gè)index記錄上次selectedIndex,只有顯示在主窗口以及不等于上次的selectedIndex的時(shí)候進(jìn)入刷新
- (void)tabbarClick
{
/**
* 先判斷是否顯示在窗口上了蜀漆,如果沒有顯示在窗口就不刷新谅河,然后判斷當(dāng)前選中的和上次是否一致,如果不一致的話才會(huì)刷新
*/
if ([self.view isShowOnWindow] && self.tabSelectIndex != self.tabBarController.selectedIndex) {
[self.tableView.mj_header beginRefreshing];
}
self.tabSelectIndex = self.tabBarController.selectedIndex;
}
當(dāng)UITableView是plain的樣式時(shí)候确丢,兩個(gè)cell之間需要有一定的間隙绷耍,這時(shí)候我們可以重寫cell的setFramefangfa
/**
* 修改父類中的東西可以在這個(gè)方法中進(jìn)行設(shè)置,這個(gè)方法會(huì)覆蓋之前所有的frame
*
* @param frame <#frame description#>
*/
- (void)setFrame:(CGRect)frame
{
frame.origin.x = 10;
frame.size.width -= 2 * frame.origin.x;
frame.size.height -= 1;
[super setFrame:frame];
}
當(dāng)一個(gè)控件從XIB創(chuàng)建的時(shí)候鲜侥,如果顯示出來(lái)的效果和你設(shè)置的不一樣褂始,建議你把a(bǔ)utoresizingMask設(shè)置為UIViewAutoresizingNone
當(dāng)一個(gè)很長(zhǎng)的圖片在一個(gè)固定大小的區(qū)域內(nèi)我們想顯示圖片的最上方內(nèi)容
我們可以裁剪圖片然后再顯示到UIImageView上面
//在這里設(shè)置長(zhǎng)圖片顯示最上方的一部分
UIGraphicsBeginImageContextWithOptions(jokesModel.picF.size, YES, 0.0);
CGFloat width = jokesModel.picF.size.width;
CGFloat height = width * image.size.height / image.size.width;
[image drawInRect:CGRectMake(0, 0, width, height)];
_imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage裁剪
- (UIImage *)circleImage
{
//NO代表透明
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
CGContextRef ref = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextAddEllipseInRect(ref, rect);
CGContextClip(ref);
//把圓畫上去
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
return image;
}
iOS開發(fā)當(dāng)中開發(fā)技巧總結(jié)
自定義了leftBarbuttonItem左滑返回手勢(shì)失效了怎么辦?
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:img
style:UIBarButtonItemStylePlain
target:self
action:@selector(onBack:)];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
ScrollView莫名其妙不能在viewController劃到頂怎么辦?
self.automaticallyAdjustsScrollViewInsets = NO;
想在代碼里改在xib里添加的layoutAttributes,但是怎么用代碼找啊?
像拉button一樣的拉你的約束.nslayoutattribute也是可以拉線的.
怎么像safari一樣滑動(dòng)的時(shí)候隱藏navigationbar?
navigationController.hidesBarsOnSwipe = Yes
導(dǎo)航條返回鍵帶的title太討厭了,怎么讓它消失!
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
本來(lái)我的statusbar是lightcontent的,結(jié)果用UIImagePickerController會(huì)導(dǎo)致我的statusbar的樣式變成黑色剃毒,怎么辦病袄?
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
怎么把我的navigationbar弄成透明的而不是帶模糊的效果?
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
怎么改變uitextfield placeholder的顏色和位置赘阀?
- 重寫方法
- (void) drawPlaceholderInRect:(CGRect)rect
{
[[UIColor blueColor] setFill];
[self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
}
- KVC
UIColor *color = [UIColor whiteColor];
_userName.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"用戶名" attributes:@{NSForegroundColorAttributeName: color}];
[_userName setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
應(yīng)該使用FOUNDATION_EXPORT還是#define來(lái)定義常量?
一般iOS我們定義常量的方法有兩種,來(lái)看下面例子
我的.h文件
FOUNDATION_EXPORT NSString * const kMyConstantString;
FOUNDATION_EXPORT NSString * const kMyConstantString2;
.m文件是這樣定義的
NSString * const kMyConstantString = @"Hello";
NSString * const kMyConstantString2 = @"World";
還有一種是常用的#define方法了
define kMyConstantString @"Hello"
有什么區(qū)別呢?
使用第一種方法在檢測(cè)字符串的值是否相等的時(shí)候更快.對(duì)于第一種你可以直接使用(stringInstance == MyFirstConstant)
來(lái)比較,而define
則使用的是這種.([stringInstance isEqualToString:MyFirstConstant])
哪個(gè)效率高,顯而易見了.第一種直接比較的是指針地址,而第二個(gè)則是一一比較字符串的每一個(gè)字符是否相等.
static inline function是干嘛的?
如果你的.m文件需要頻繁調(diào)用一個(gè)函數(shù),可以用static inline來(lái)聲明,這相當(dāng)于把函數(shù)體當(dāng)做一個(gè)大號(hào)的宏定義.不過(guò)這也不是百分之百有效,到底能不能把函數(shù)體轉(zhuǎn)換為大號(hào)宏定義來(lái)用要看編譯器心情,它要是覺得你的方法太復(fù)雜,他就不轉(zhuǎn)了.他直接調(diào)用函數(shù).
類似這種簡(jiǎn)單函數(shù)他肯定是樂意的.
static inline CGRect ScaleRect(CGRect rect, float n)