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

Source: Internet
Author: User

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

 

1: understanding of the initWithFrame Method

1. What is the initWithFrame method? The initWithFrame method is used to initialize and return a new view object based on the specified CGRect (size ). Of course, there are also initWithFrame methods for other UI objects. However, we use UIView as an example to clarify the initWithFrame method. 2. When can I use the initWithFrame method? To put it simply, we declare in programming that the initWithFrame method is used when creating a UIView object. Here, we must clarify two methods to initialize the UIView. A. Use Interface Builder. In this way, the nib file is used. We usually call the drag control method. In actual programming, if we use Interface Builder to create a UIView object. (That is, using the Drag Control method), The initWithFrame method will not be called. Because the nib file already knows how to initialize the View. (Because, when we drag this view, we have defined attributes such as length, width, and background ). At this time, the initWithCoder method will be called. We can use the initWithCoder method to redefine the attributes we have set in nib. B. Use programming methods. This is how we declare a subclass of UIView and write code manually. In actual programming, we use programming to create a UIView or a subclass of the UIView. At this time, the initWithFrame method will be called to instantiate the UIView. Note that if you overload the initWithFrame method in the subclass, you must first call the initWithFrame method of the parent class. Initialize the custom UIView subclass. For example:-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; // first call the initWithFrame method of the parent class if (self) {// customize the initialization operation of this class (UIView subclass. _ ScrollView = [[UIScrollView alloc] initWithFrame: self. bounds]; [_ scrollView setFrame: CGRectMake (0, 0,320,480)]; _ scrollView. contentSize = CGSizeMake (320*3,480); [self addSubview: _ scrollView];} return self ;}

2: layoutSubviews Summary

LayoutSubviews will be called in the following cases: a and init initialization will not trigger layoutSubviews, but when initWithFrame is used for initialization, when the rect value is not CGRectZero, it will also trigger B and addSubview. It will trigger layoutSubviewsc and set the view Frame to trigger layoutSubviews, of course, the premise is that the frame value is changed before and after setting d. Rolling a UIScrollView will trigger layoutSubviewse, rotating the Screen will trigger the layoutSubviews event on the parent UIView f, and changing the size of a UIView will also trigger layoutSubviews on the parent UIView, you need to call this function when you adjust the position of the subview within a class. This means that if you want to set the location of the subviews externally, do not rewrite it. LayoutSubviews la views subviews. The layoutSubviews method is called to refresh the sub-object layout before drawRect-layoutSubviews method: by default, this method does not do anything. You need to override the sub-class-setNeedsLayout method: the layoutIfNeeded method is called asynchronously to refresh the layout without refreshing it immediately. However, layoutSubviews will be called-layoutIfNeeded method: if there are tags that need to be refreshed, call layoutSubviews immediately for layout (if there is no tag, layoutSubviews will not be called) If you want to refresh immediately, call [view setNeedsLayout] first to set the tag as needed layout, then, call [view layoutIfNeeded] to display the layout before the view is displayed for the first time. You can directly call [view layoutIfNeeded]

3: When the unit row has other controls, the control highlight issue occurs during row selection.

Here, the accessoryView in the cell is put into UIButton. When the row is selected, UIButton is also highlighted in the selected strong State. The following processing can solve the problem @ interface UCaiTableViewCell: UITableViewCell @ end @ implementation progress @ synthesize finished = _ success;-(void) setHighlighted :( BOOL) highlighted animated :( BOOL) animated {[super healthy: highlighted animated: animated]; if (highlighted) {[(UIButton *) self. accessoryView setHighlighted: NO] ;}}-(void) setSelected :( BOOL) selected animated :( BOOL) animated {[super setSelected: selected animated: animated]; if (selected) {[(UIButton *) self. accessoryView setHighlighted: NO];}

4: Remove UIButton highlight

Inherit from UIButton and then rewrite the setHighlighed method. Nothing is written in it ;. h file: # import <UIKit/UIKit. h> @ interface HWEmotionTabBarButton: UIButton@end.M file: # import "HWEmotionTabBarButton. h "@ implementation HWEmotionTabBarButton-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// set the text color [self setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; [self setTitleColor: [UIColor darkGrayColor] forState: UIControlStateDisabled]; // set the font self. titleLabel. font = [UIFont systemFontOfSize: 13];} return self;}-(void) setHighlighted :( BOOL) highlighted {// all operations performed by the highlighted button are gone} @ end

5: encapsulation of a tab

. H file content # import <UIKit/UIKit. h> typedef enum {batch, // The latest HWEmotionTabBarButtonTypeDefault, // The default HWEmotionTabBarButtonTypeEmoji, // emoji batch, // Lang Xiaohua} HWEmotionTabBarButtonType; @ class HWEmotionTabBar; @ protocol initialize <NSObject> @ optional-(void) emotionTabBar :( HWEmotionTabBar *) tabBar didSelectButton :( optional) buttonType; @ end @ interface HWEmotionTabBar: UIView @ property) id <HWEmotionTabBarDelegate> delegate; @ end note: here we mainly introduce @ class. M file content: @ interface HWEmotionTabBar () @ property (nonatomic, weak) HWEmotionTabBarButton * selectedBtn; @ end @ implementation HWEmotionTabBar-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {[self setupBtn: @ "Recent" buttonType: HWEmotionTabBarButtonTypeRecent]; [self setupBtn: @ "default" buttonType: dependencies]; // [self btnClick: [self setupBtn: @ "default" buttonType: HWEmotionTabBarButtonTypeDefault]; [self setupBtn: @ "Emoji" buttonType: callback]; [self setupBtn: @ "Lang Xiaohua" buttonType: HWEmotionTabBarButtonTypeLxh];} return self;}/*** create a button ** @ param title button text */-(HWEmotionTabBarButton *) setupBtn :( NSString *) title buttonType :( HWEmotionTabBarButtonType) buttonType {// create button HWEmotionTabBarButton * btn = [[HWEmotionTabBarButton alloc] init]; [btn addTarget: self action: @ selector (btnClick :) forControlEvents: UIControlEventTouchDown]; btn. tag = buttonType; [btn setTitle: title forState: UIControlStateNormal]; [self addSubview: btn]; // set the background image NSString * image = @ "compose_emotion_table_mid_normal "; NSString * selectImage = @ "compose_emotion_table_mid_selected"; if (self. subviews. count = 1) {image = @ "compose_emotion_table_left_normal"; selectImage = @ "compose_emotion_table_left_selected";} else if (self. subviews. count = 4) {image = @ "success"; selectImage = @ "success";} [btn setBackgroundImage: [UIImage imageNamed: image] forState: UIControlStateNormal]; [btn setBackgroundImage: [UIImage imageNamed: selectImage] forState: UIControlStateDisabled]; return btn;}-(void) layoutSubviews {[super layoutSubviews]; // frame NSUInteger btnCount = self. subviews. count; CGFloat btnW = self. width/btnCount; CGFloat btnH = self. height; for (int I = 0; I <btnCount; I ++) {HWEmotionTabBarButton * btn = self. subviews [I]; btn. y = 0; btn. width = btnW; btn. x = I * btnW; btn. height = btnH ;}}- (void) setDelegate :( id <HWEmotionTabBarDelegate>) delegate {_ delegate = delegate; // select the "default" button [self btnClick :( HWEmotionTabBarButton *) [self viewWithTag: HWEmotionTabBarButtonTypeDefault];}/*** click */-(void) btnClick :( HWEmotionTabBarButton *) btn {// convert the selected effect self. selectedBtn. enabled = YES; btn. enabled = NO; self. selectedBtn = btn; // notification proxy if ([self. delegate respondsToSelector: @ selector (emotionTabBar: didSelectButton :)]) {[self. delegate emotionTabBar: self didSelectButton: btn. tag] ;}}@ end note: After the control is added, self. subviews. the value of count starts from 1, while the value of setDelegate is used to display the default value selected. Because of enumeration, You can accurately locate the UIButton that you want to set the default value, the status of UIControlStateDisabled is used to save the currently selected UIButton by setting enabled and setting an attribute in the code, convert and assign values to event btnClick;

 

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.