Basic knowledge of IOS development-fragment 17, basic knowledge of ios-Fragment

Source: Internet
Author: User

Basic knowledge of IOS development-fragment 17, basic knowledge of ios-Fragment

 

1: differences between contentSize, contentInset, and contentOffset

ContentSize is an attribute in scrollview. It represents the display area in scrollview. If there is a scrollview, its frame is (320,480, 320,960), and its contentSize is ). that is to say, the size of the entire scrollview is (320,960). You need to slide scrollview up and down to view the content after (320,480. ContentOffset is the offset of the vertex in the current display area of the scrollview to the frame vertex. For example, if you pull to the bottom of the last example, contentoffset is (0,-480 ), that is, y is offset by 480. contentInset is contentView in scrollview. frame. origin and scrollview. frame. the relationship between origin and contentView. For example, if the frame of contentView is (320,480,), contentInset is (0, 30). You can also set the relationship between top, bottom, and bottom.

 

2: install other Simulator on the IOS Virtual Machine

Install the downloaded dmg. here, the offline installation of the iOS7.0 simulator is used as an example. In other versions, download ios_7_0_simulator.dmg and open the dmg file. You can see the installation package iPhoneSimulatorSDK7_0.pkg and install the installation package using the installer, the/Platforms/iPhoneSimulator in the selected partition is installed by default. in the platform/Developer/SDKs/iPhoneSimulator7.0.sdk directory, completely exit Xcode and copy or move the entire directory of the installed iPhoneSimulator7.0.sdk to/Applications/Xcode. app/Contents/Developer/Platforms/iPhoneSimulator. platform/Developer/SDKs directory, (Xcode. right-click the app to "display the package content") Restart Xcode to use the corresponding version of the simulation. Tool for Development and debugging. There is also a simple way to install the SDK offline. You can copy and use the previously installed version of Xcode, such as Xcode 5.0.2 and iOS simulators. The directories are all in the same directory. app/Contents/Developer/Platforms/iPhoneSimulator. in platform/Developer/SDKs. In this way, you no longer need to download the offline installation package.

 

3: inputaccessoryview and inputview in the input box

UITextFields and UITextView have an inputAccessoryView attribute. You can set this attribute when you want to display a custom view on the keyboard. The view you set is automatically displayed with the keyboard. It should be noted that the custom view should neither be in other view layers nor be subviews of other views. In fact, you only need to assign the attribute inputAccessoryView to the custom view, and do not perform other unnecessary operations. Inputview is the keyboard view. When it is nil, the default keyboard is displayed. instance 1 (set a toolbar above the keyboard):-(void) createKeyboardTool {keyboardTool = [[UIToolbar alloc] initWithFrame: CGRectMake (kZero, kZero, kScreenW, 44.0f)]; NSMutableArray * myToolBarItems = [javasarray]; // create the button above the keyboard toolbar and set the Click Event UIBarButtonItem * cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: Invalid target: self action: @ selector (cancelAction)]; UIBarButtonItem * space = [[UIBarButtonItem alloc] lower: lower target: self action: @ selector (saveAction)]; UIBarButtonItem * saveBtn = [[UIBarButtonItem alloc] lower: lower target: self action: @ selector (saveAction)]; [myToolBarItems addObject: cancelBtn]; [myToolBarItems addObject: space]; [myToolBarItems addObject: saveBtn]; keyboardTool. items = myToolBarItems;} // inputAccessoryView: sets the toolbar displayed on the top of the keyboard; inputView: Custom keyboard commentTextView = [[UITextView alloc] initWithFrame: CGRectMake (kZero, kZero, kScreenW, (200)]; [commentTextView becomeFirstResponder]; commentTextView. inputAccessoryView = keyboardTool; instance 2 (modify the keyboard view and switch the custom View to the built-in view):/***** switch the keyboard */-(void) switchKeyboard {// self. textView. inputView = nil: if (self. textView. inputView = nil) {// switch to the custom emoticons keyboard emtionKeyboard as a view self. textView. inputView = self. emotionKeyboard; // display the keyboard button self. toolbar. showKeyboardButton = YES;} else {// switch to the built-in keyboard self. textView. inputView = nil; // display the emoticons button self. toolbar. showKeyboardButton = NO;} // start to switch the keyboard. This is a fixed self. switchingKeybaord = YES; // exit the keyboard [self. textView endEditing: YES]; dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (0.1 * NSEC_PER_SEC), interval (), ^ {// pop-up keyboard to make it implement [self. textView becomeFirstResponder]; // end switch the keyboard self. switchingKeybaord = NO;});}/*** call (display, hide, etc.) when the frame of the keyboard changes */-(void) keyboardWillChangeFrame :( NSNotification *) notification {// if the keyboard is being switched, do not execute the following code if (self. switchingKeybaord) return; NSDictionary * userInfo = notification. userInfo; // animation duration double duration = [userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // The frame CGRect keyboardF of the keyboard = [userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue]; // execute the animation [UIView animateWithDuration: duration animations: ^ {// Y value of the toolbar = Y value of the keyboard-height of the toolbar if (keyboardF. origin. y> self. view. height) {// The Y value of the keyboard has far exceeded the height of the controller view self. toolbar. y = self. view. height-self. toolbar. height;} else {self. toolbar. y = keyboardF. origin. y-self. toolbar. height;}];}

 

4: Modify the cannel cancellation text in UISearchBar

-(UISearchBar *) mySearchBar {if (_ mySearchBar = nil) {_ mySearchBar = [[UISearchBar alloc] init]; _ mySearchBar. showsCancelButton = YES; _ mySearchBar. delegate = self; [_ mySearchBar sizeToFit]; [_ mySearchBar setPlaceholder: @ "Please input"]; [_ mySearchBar setY: 20]; // process the cannel text display for (id item in [_ mySearchBar subviews]) {for (id cc in [item subviews]) {if ([cc isKindOfClass: [UIButton class]) {UIButton * btn = (UIButton *) cc; [btn setTitle: @ "cancel" forState: UIControlStateNormal] ;}}} return _ mySearchBar ;} if the cancellation is displayed only after the click is obtained, you can set it in the delegate:/*** @ author wujunyang, 15-06-24 11:06:44 ** @ brief to modify the display text of cancel, you must first set showscancelButton to 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: @ "cancel" forState: UIControlStateNormal] ;}}}

 

5: push jump and jump back when adding controls to navigationController

Add a control to the subpage navigationController. During the rollback, the control cannot be deleted by itself. Therefore, you must manually add a control to remove the added control from the nav, otherwise, the control on the Child page will be overlapped and displayed on the nav of the parent page. For example, in the next instance: In viewDidLoad, // load the control [self. navigationController. view addSubview: self. mySearchBar]; (void) viewWillDisappear :( BOOL) animated {// This sentence can also be written before the return hop [self. mySearchBar removeFromSuperview]; [super viewWillDisappear: animated];}

 

6: The entire view is clicked to shrink the keyboard

-(Void) viewDidLoad {[super viewDidLoad]; UITapGestureRecognizer * tapGr = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (viewTapped :)] // without the Button in the view, the ToucheUpInside event tapGr may not be triggered. cancelsTouchesInView = NO; [self. view addGestureRecognizer: tapGr];}-(IBAction) BtnAction :( id) sender {NSLog (@ "% @", self. myTextField. text);}-(void) viewTapped :( UITapGestureRecognizer *) tapGr {[self. myTextField resignFirstResponder];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning];} @ end

 

7: calls for third-party plug-ins that are mrc and projects that are arc

Third-party plug-ins. set the m file. For the project targets-build phases-compile sources settings-fno-objc-arc, some are double stars, such as PLTexture ** previewTextures. Under the arc, modify it: PLTexture * _ unsafe_unretained * previewTextures;

 

8: How to Implement keyboard contraction and layout of notifications

/*** Add toolbar */-(void) setupToolbar {// 1. add the toolbar 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. when the frame (position) of the listening keyboard is about to change, the UIKeyboardWillChangeFrameNotification // the keyboard is about to pop up, And the UIKeyboardWillShowNotification [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (keyboardWillShow :) name: UIKeyboardWillShowNotification object: nil]; // If the keyboard is about to be hidden, UIKeyboardWillHideNotification [[nsicationicationcenter defacenter] addObserver: self selector: @ selector (keyboardWillHide :) name: UIKeyboardWillHideNotification object: nil];} # pragma mark-keyboard processing/*** the keyboard is about to be hidden */-(void) keyboardWillHide :( NSNotification *) note {// 1. time required for keyboard pop-up CGFloat duration = [note. userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 2. animation [UIView animateWithDuration: duration animations: ^ {self. toolbar. transform = CGAffineTransformIdentity;}];}/*** the keyboard is about to pop up */-(void) keyboardWillShow :( NSNotification *) note {// 1. time required for keyboard pop-up CGFloat duration = [note. userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 2. animation [UIView animateWithDuration: duration animations: ^ {// retrieve the keyboard height CGRect keyboardF = [note. userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardH = keyboardF. size. height; self. toolbar. transform = CGAffineTransformMakeTranslation (0,-keyboardH);}] ;}// notification to be released-(void) dealloc {[[nsicationicationcenter ultcenter center] removeObserver: self];} note: [self. textView resignFirstResponder]; discard the click and listen to changes in the input content: // 2. listen for textView text changes [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (textChange) name: UITextViewTextDidChangeNotification object: textView];

 

9: encapsulate an instance of uivew with a key Toolbar

. H file content: # import <UIKit/UIKit. h> @ class IWComposeToolbar; typedef enum {delimiter, delimiter} delimiter; @ protocol delimiter <NSObject> @ optional-(void) composeToolbar :( IWComposeToolbar *) toolbar didClickButton :( IWComposeToolbarButtonType) butonType; @ end @ interface IWComposeToolbar: UIView @ property (weak, nonatomic) id <IWComposeToolbarDelegate> delegate; @ end. m file content: # import "IWComposeToolbar. h "@ implementation IWComposeToolbar-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// 1. set the background self. backgroundColor = [UIColor colorWithPatternImage: [UIImage imageWithName: @ "compose_toolbar_background"]; // 2. add the button [self addButtonWithIcon: @ "Courier" highIcon: @ "Courier" tag: Ghost]; [self addButtonWithIcon: @ "compose_toolbar_picture" highIcon: @ "Courier" tag: Ghost]; [self addButtonWithIcon: @ "compose_mentionbutton_background" highIcon: @ "regular" tag: Regular]; [self addButtonWithIcon: @ "regular" highIcon: @ "regular" tag: Regular]; [self addButtonWithIcon: @ "Courier" highIcon: @ "Courier" 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];}/*** click the listener 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.