IOS開發基礎知識--片段17,ios基礎知識--片段

來源:互聯網
上載者:User

IOS開發基礎知識--片段17,ios基礎知識--片段

 

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位移了480contentInset 是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;    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];    [button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];    [button setImage:[UIImage imageWithName:highIcon] 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.