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

Today is Laba Festival丨The Laba garlic turns green, is it really spoiled?

Today is the eighth day of the twelfth lunar mont...

Urgent reminder again! Don't take photos of these bugs

During the past May Day holiday The most popular ...

36 Strategies of Sales Closing eBook

36 Strategies of Sales Closing eBook The first se...

The pineapples on the tables of European nobles were once rented

For someone who likes pineapples, even if the pin...

What would happen to humanity if the Earth lost its atmosphere?

Generally speaking, the less noticeable something...

Business Analysis Skills Course

Course Features 1. Aiming to "solve various ...

Do shared electric bikes appearing on Beijing streets again have a future?

Recently, a group of uniform yellow electric bicy...

WeX5 cross-terminal mobile development framework V3.1 official version

[[132317]] 1. Provide a lightweight and highly sc...