iOS scams collection

iOS scams collection

When I was making my first iOS app, I encountered many difficulties along the way. Fortunately, I solved them all with the help of Google and StackOverflow. I don’t know if it is the best practice.

Hide the Tab bar

In an app that divides modules with a Tab bar, some non-first-level interfaces do not need a tab bar at the bottom. You only need to add a statement to hide the tab bar in the viewWillAppear: of the ViewController:

  1. - ( void )viewWillAppear:(BOOL)animated {
  2. [ super viewWillAppear:animated];
  3. self.tabBarController.tabBar.hidden = YES;
  4. }

However, a better approach is to set the property hidesBottomBarWhenPushed to YES before pushing a ViewController:

  1. SomeViewController *svc = [SomeViewController new ];
  2. svc.hidesBottomBarWhenPushed = YES;
  3. [self.navigationController pushViewController:svc animated:YES];

Calculating UIScrollView's ContentSize

Some UIScrollView content is dynamically increased or decreased, which requires recalculating the ContentSize. Add the following code after changing the content:

  1. -( void )resizeScrollViewContentSize {
  2. [self layoutIfNeeded];
  3. CGRect contentRect = CGRectZero;
  4. for (UIView *view in self.subviews) {
  5. contentRect = CGRectUnion(contentRect, view.frame);
  6. }
  7. self.contentSize = CGSizeMake(contentRect.size.width, contentRect.size.height);
  8. }

It seems that layoutIfNeeded must be executed before calculation, otherwise some sub views will not be laid out yet.

Calculate the height of multiple lines of text

UILabel and UITextView can display multiple lines of text. If the string is obtained dynamically, you need to calculate the height of the entire text (the width is usually fixed). At this time, you need to use the boundingRectWithSize: options: attributes: context: API (new in iOS7). In order to facilitate calling in my own project, I encapsulated it as follows:

  1. + (CGRect)stringRect:(NSString *)string fontSize:(CGFloat)fontSize constraintWidth:(CGFloat)width constraintHeight:(CGFloat)height {
  2. UIFont *font = [UIFont systemFontOfSize:fontSize];
  3. CGSize constraint = CGSizeMake(width, height);
  4. NSDictionary *attributes = @{NSFontAttributeName : font};
  5. return [string boundingRectWithSize:constraint
  6. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  7. attributes:attributes
  8. context:nil];
  9. }

Remove leading and trailing spaces from a string

The string entered in UITextField often needs to be trimmed, which requires the following code:

  1. NSString *result = [self..nameTextField.text
  2. stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

Monitor UITextView input and display the number of words in real time

First, you need to conform to UITextViewDelegate and display the number of words in the current UITextView in UILabel in textViewDidChange::

  1. - ( void )textViewDidChange:(UITextView *)textView {
  2. _countLabel.text = [NSString stringWithFormat:@ "%lu" , (unsigned long )textView.text.length];
  3. [self setNeedsDisplay];
  4. }

Set the maximum input length of UITextView

Implement the textView:shouldChangeTextInRange: method in UITextViewDelegate:

  1. -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  2. // if (range.location >= kMaxTextLength) { This will cause the maximum length to be invalid after moving the cursor  
  3. if (textView.text.length >= kMaxTextLength) {
  4. return NO;
  5. }
  6.  
  7. return YES;
  8. }

<<:  Google Now employees leave due to dissatisfaction with new CEO's restructuring plan

>>:  Some tips for becoming a hardcore programmer

Recommend

How to prevent cardiovascular and cerebrovascular diseases in summer?

Author: Sun Taixin, Chief Physician, Beijing Elec...

360 Premium Advertisements Take Over the Screen with New Play

In recent years, when people talk about online ma...

Tips for creating short video scripts

A script is the outline needed for shooting a sho...

Xiaomi App Store, teach you how to use it in 30 minutes!

I have been in the Internet industry for two year...

The 10 most common problems on Android

[[156039]] Here are the most common React native ...

The whole network is taking on the challenge? Urgent reminder!

The girls in high buns and green clothes walked s...

Himalaya FM-AARRR traffic funnel model analysis report!

With the rise of knowledge payment, audio payment...

Will I be iodine deficient if I stop eating seafood?

In the age of universal iodine supplementation, p...

Talk about the channels, methods and techniques for promoting To B products

We haven’t talked about the promotion methods of ...

In Zhaosu, Xinjiang, be a "rainbow chaser"

The rainbow has been a symbol of beauty since anc...