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

This generation of young people rely on "sound massage" to help them sleep

Difficulty falling asleep, shallow sleep, dreamin...

Cocos Developer Salon——Cocos Studio V2.1 Open Plan

In the era of mobile Internet, the mobile game in...

E-commerce short video pyramid creation method (Part 1)

Many Douyin merchants always face the dilemma of ...

How to create your own entry in Baidu Encyclopedia? Is there any method?

Baidu Encyclopedia started in 2006. When it was f...

"Without Office, WPS will make more money"

How popular is WPS in the country? An office soft...