1:contentSize、contentInset和contentOffset區別
contentSize 是scrollview中的一個屬性,它代表scrollview中的可顯示區域,假如有一個scrollview,它的frame為(0,0,320,480),而它的contentSize為(320,960).也就是說,這個scrollview整個內容的大小為(320,960),要通過上下滑動scrollview來查看(320,480)後的內容。
contentOffset 是scrollview當前顯示區域頂點相對於frame頂點的偏移量,比如上個例子你拉到最下面,contentoffset就是(0 ,-480),也就是y偏移了480
contentInset 是scrollview中contentView.frame.origin與scrollview.frame.origin的關系,比如contentView的frame為(0,30,320,480),那麼contentInset則為(0, 30),它也可以設置上下左右
2:IOS虛擬器安裝其它Simulator
下載後的dmg安裝.這裡主要以iOS7.0模擬器的離線安裝為例進行說明,其他版本以此類推:
下載ios_7_0_simulator.dmg後打開dmg文件,可以看到安裝包iPhoneSimulatorSDK7_0.pkg,使用安裝器安裝此安裝包,默認會安裝在所選分區的/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk目錄下,完全退出Xcode後將剛才安裝的iPhoneSimulator7.0.sdk整個目錄復制或移動到/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs目錄下即可,(Xcode.app右鍵可以"顯示包內容“)重新啟動Xcode一般就可以使用相應版本的模擬器進行開發和調試了。
離線安裝還有一個簡單的辦法就是將以前安裝過的舊版本的Xcode如Xcode5.0.2下面已經安裝好了的iOS模擬器直接復制過來使用,目錄位置都一樣,都是在Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs裡面。這樣就不用再下載離線安裝包了。
3:輸入框中的inputaccessoryview和inputview
UITextFields和UITextView有一個inputAccessoryView的屬性,當你想在鍵盤上展示一個自定義的view時,你就可以設置該屬性。你設置的view就會自動和鍵盤keyboard一起顯示了。需要注意的是,你所自定義的view既不應該處在其他的視圖層裡,也不應該成為其他視圖的子視圖。其實也就是說,你所自定義的view只需要賦給屬性inputAccessoryView就可以了,不要再做其他多余的操作。
inputview則是鍵盤視圖,當其為nil時則彈出的是系統默認的鍵盤;
實例一(給鍵盤上方設置一個工具條的方式):
- (void)createKeyboardTool
{
keyboardTool = [[UIToolbar alloc] initWithFrame: CGRectMake(kZero, kZero, kScreenW, 44.0f)];
NSMutableArray *myToolBarItems = [NSMutableArray array];
//創建鍵盤工具條上面的按鈕,並設置點擊事件
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(cancelAction)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(saveAction)];
UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(saveAction)];
[myToolBarItems addObject:cancelBtn];
[myToolBarItems addObject:space];
[myToolBarItems addObject:saveBtn];
keyboardTool.items = myToolBarItems;
}
//inputAccessoryView:設置鍵盤頂部顯示的工具條;inputView:自定義鍵盤
commentTextView = [[UITextView alloc]initWithFrame:CGRectMake(kZero, kZero, kScreenW, 200)];
[commentTextView becomeFirstResponder];
commentTextView.inputAccessoryView = keyboardTool;
實例二(修改鍵盤視圖,進行切換自定義視圖跟系統自帶視圖):
/**
* 切換鍵盤
*/
- (void)switchKeyboard
{
// self.textView.inputView == nil : 使用的是系統自帶的鍵盤
if (self.textView.inputView == nil) {
// 切換為自定義的表情鍵盤 emtionKeyboard為一個視圖
self.textView.inputView = self.emotionKeyboard;
// 顯示鍵盤按鈕
self.toolbar.showKeyboardButton = YES;
} else {
// 切換為系統自帶的鍵盤
self.textView.inputView = nil;
// 顯示表情按鈕
self.toolbar.showKeyboardButton = NO;
}
// 開始切換鍵盤 這個是為固定用的
self.switchingKeybaord = YES;
// 退出鍵盤
[self.textView endEditing:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 彈出鍵盤,讓其慢點實現
[self.textView becomeFirstResponder];
// 結束切換鍵盤
self.switchingKeybaord = NO;
});
}
/**
* 鍵盤的frame發生改變時調用(顯示、隱藏等)
*/
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
// 如果正在切換鍵盤,就不要執行後面的代碼
if (self.switchingKeybaord) return;
NSDictionary *userInfo = notification.userInfo;
// 動畫的持續時間
double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 鍵盤的frame
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 執行動畫
[UIView animateWithDuration:duration animations:^{
// 工具條的Y值 == 鍵盤的Y值 - 工具條的高度
if (keyboardF.origin.y > self.view.height) { // 鍵盤的Y值已經遠遠超過了控制器view的高度
self.toolbar.y = self.view.height - self.toolbar.height;
} else {
self.toolbar.y = keyboardF.origin.y - self.toolbar.height;
}
}];
}
4:修改UISearchBar中關於cannel取消的文字
-(UISearchBar *)mySearchBar
{
if (_mySearchBar==nil) {
_mySearchBar=[[UISearchBar alloc]init];
_mySearchBar.showsCancelButton=YES;
_mySearchBar.delegate=self;
[_mySearchBar sizeToFit];
[_mySearchBar setPlaceholder:@"請輸入"];
[_mySearchBar setY:20];
//處理cannel的文字顯示
for (id item in [_mySearchBar subviews]) {
for(id cc in [item subviews])
{
if ([cc isKindOfClass:[UIButton class]]) {
UIButton *btn=(UIButton *)cc;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
}
}
return _mySearchBar;
}
如果是獲得瞧點才顯示出取消可以在這個委托裡面進行設置:
/**
* @author wujunyang, 15-06-24 11:06:44
*
* @brief 修改cancel的顯示文字 必先把showscancelButton設置為yes
* @param searchBar <#searchBar description#>
*/
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton=YES;
for (id item in [searchBar subviews]) {
for(id cc in [item subviews])
{
if ([cc isKindOfClass:[UIButton class]]) {
UIButton *btn=(UIButton *)cc;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
}
}
5:關於navigationController中增加控件時push跳轉及跳回
在子頁navigationController增加控件,回跳時它是沒辦法自個銷除,所以要手動增加一個銷除nav所增加的控件,否則子頁的那個控件會被重疊顯示在父頁的nav上;如下一個實例:
在viewDidLoad裡
//加載控件
[self.navigationController.view addSubview:self.mySearchBar];
(void)viewWillDisappear:(BOOL)animated {
//這句也可以寫在回跳前
[self.mySearchBar removeFromSuperview];
[super viewWillDisappear:animated];
}
6:整個視圖點擊都對鍵盤進行收縮
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGr=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
//如果沒有這句在view中的Button等可能無法觸發ToucheUpInside事件
tapGr.cancelsTouchesInView=NO;
[self.view addGestureRecognizer:tapGr];
}
- (IBAction)BtnAction:(id)sender {
NSLog(@"%@",self.myTextField.text);
}
-(void)viewTapped:(UITapGestureRecognizer *)tapGr
{
[self.myTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
7:針對第三方插件為mrc,而工程為arc的調用
對第三方插件的.m文件進行設置,工程targets-build phases-compile sources 設置-fno-objc-arc
有些是雙星如 PLTexture **previewTextures;
在arc下面則要修改成:PLTexture * __unsafe_unretained *previewTextures;
8:通知的方式實現鍵盤的收縮布局問題
/**
* 添加工具條
*/
- (void)setupToolbar
{
// 1.添加工具條
IWComposeToolbar *toolbar = [[IWComposeToolbar alloc] init];
toolbar.delegate = self;
CGFloat toolbarH = 35;
CGFloat toolbarW = self.view.width;
CGFloat toolbarY = self.view.height - toolbarH;
toolbar.frame = CGRectMake(0, toolbarY, toolbarW, toolbarH);
[self.view addSubview:toolbar];
self.toolbar = toolbar;
// 2.監聽鍵盤的彈出和隱藏
// 鍵盤的frame(位置)即將改變, 就會發出UIKeyboardWillChangeFrameNotification
// 鍵盤即將彈出, 就會發出UIKeyboardWillShowNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 鍵盤即將隱藏, 就會發出UIKeyboardWillHideNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark - 鍵盤處理
/**
* 鍵盤即將隱藏
*/
- (void)keyboardWillHide:(NSNotification *)note
{
// 1.鍵盤彈出需要的時間
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 2.動畫
[UIView animateWithDuration:duration animations:^{
self.toolbar.transform = CGAffineTransformIdentity;
}];
}
/**
* 鍵盤即將彈出
*/
- (void)keyboardWillShow:(NSNotification *)note
{
// 1.鍵盤彈出需要的時間
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 2.動畫
[UIView animateWithDuration:duration animations:^{
// 取出鍵盤高度
CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardH = keyboardF.size.height;
self.toolbar.transform = CGAffineTransformMakeTranslation(0, - keyboardH);
}];
}
//通知要銷掉
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
注意:[self.textView resignFirstResponder];放棄瞧點
還有可以監聽輸入內容的變化: // 2.監聽textView文字的改變
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:textView];
9:封裝一個uivew帶有按鍵工具欄的實例
.h文件內容:
#import <UIKit/UIKit.h>
@class IWComposeToolbar;
typedef enum {
IWComposeToolbarButtonTypeCamera,
IWComposeToolbarButtonTypePicture,
IWComposeToolbarButtonTypeMention,
IWComposeToolbarButtonTypeTrend,
IWComposeToolbarButtonTypeEmotion
} IWComposeToolbarButtonType;
@protocol IWComposeToolbarDelegate <NSObject>
@optional
- (void)composeToolbar:(IWComposeToolbar *)toolbar didClickButton:(IWComposeToolbarButtonType)butonType;
@end
@interface IWComposeToolbar : UIView
@property (weak, nonatomic) id<IWComposeToolbarDelegate> delegate;
@end
.m文件內容:
#import "IWComposeToolbar.h"
@implementation IWComposeToolbar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 1.設置背景
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];
// 2.添加按鈕
[self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:IWComposeToolbarButtonTypeCamera];
[self addButtonWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:IWComposeToolbarButtonTypePicture];
[self addButtonWithIcon:@"compose_mentionbutton_background" highIcon:@"compose_mentionbutton_background_highlighted" tag:IWComposeToolbarButtonTypeMention];
[self addButtonWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:IWComposeToolbarButtonTypeTrend];
[self addButtonWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted" tag:IWComposeToolbarButtonTypeEmotion];
}
return self;
}
- (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(IWComposeToolbarButtonType)tag
{
UIButton *button = [[UIButton alloc] init];
button.tag = tag;
;
forState:UIControlStateNormal];
forState:UIControlStateHighlighted];
[self addSubview:button];
}
/**
* 監聽按鈕點擊
*/
- (void)buttonClick:(UIButton *)button
{
if ([self.delegate respondsToSelector:@selector(composeToolbar:didClickButton:)]) {
[self.delegate composeToolbar:self didClickButton:button.tag];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
int count = self.subviews.count;
CGFloat buttonW = self.width / count;
CGFloat buttonH = self.height;
for (int i = 0; i<count; i++) {
UIButton *button = self.subviews[i];
CGFloat buttonX = buttonW * i;
button.frame = CGRectMake(buttonX, 0, buttonW, buttonH);
}
}
@end