IOS Pits Collection

Source: Internet
Author: User

In doing their first IOS app, all the way to meet a lot of difficulties, fortunately by Google and StackOverflow have solved, I do not know whether it is the best practice, in order to catch up as long as work is good.

Hide Tab Bar

Some non-primary interfaces in apps with tab bar are not required at the bottom of the tab bar, just in the Viewcontroller viewwillappear: to add the Settings tab bar hidden statement:

- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated];    self.tabBarController.tabBar.hidden = YES;}
Calculate the contentsize of Uiscrollview

Some Uiscrollview content is dynamically added and subtracted, which requires recalculation of contentsize, adding the following code after changing the content:

-(void) resizescrollviewcontentsize {[Self layoutifneeded];    CGRect contentrect = Cgrectzero;    For (UIView *view in self.subviews) {contentrect = Cgrectunion (Contentrect, view.frame); } self.contentsize = Cgsizemake (ContentRect.size.width, contentRect.size.height);}  

It looks like it has to be done before the calculation layoutIfNeeded , otherwise some sub view is not well laid out.

Set the text color of the status bar

After iOS7, the background color of the status bar becomes transparent, and the system automatically changes the font color (black and white) of the status bar based on the app's color. But this auto-changed font color does not necessarily match with all the apps, such as the theme color of our app is a little shallow lost blue, but the system-matching status bar font color is black, it looks very uncomfortable, so it is forced to change to white.

  1. First, add a key item to the Information property list in Info.plist View controller-based status bar appearance , whose Type is set to Boolean,value to NO.
  2. Then AppDelegate.m add in the application:didFinishLaunchingWithOptions: :
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
Calculate the height of multiple lines of text

uilabel and Uitextview can display multiple lines of text, if the string is dynamically obtained, you need to calculate the height of the entire text (the width is generally fixed), then use the boundingrectwithsize:options:attributes:context: this API (iOS7 new). In order to facilitate the invocation of the project, I encapsulated a bit:

+ (CGRect)stringRect:(NSString *)string fontSize:(CGFloat)fontSize constraintWidth:(CGFloat)width constraintHeight:(CGFloat)height { UIFont *font = [UIFont systemFontOfSize:fontSize]; CGSize constraint = CGSizeMake(width, height); NSDictionary *attributes = @{NSFontAttributeName : font}; return [string boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil];}
Remove whitespace from the tail of the string

For strings entered in Uitextfield, it is often necessary to trim and use the following code:

NSString *result = [self..nameTextField.text                          stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Monitor the input of uitextview and display the word count in real time

The first thing to do is conform (compliance? Realize? ) to display the number of words in the UITextViewDelegate textViewDidChange: current Uitextview in UILabel:

- (void)textViewDidChange:(UITextView *)textView {    _countLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)textView.text.length];    [self setNeedsDisplay];}
Set the maximum input length for the Uitextview

realization uitextviewdelegate textview:shouldchangetextinrange: Method:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {   // if (range.location >= kMaxTextLength) { 这样会导致移动光标后再输入最大长度就失效    if(textView.text.length >= kMaxTextLength) {        return NO;    }    return YES;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Pits Collection

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.