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

Android turns 10: What's left of the original OS?

[[244605]] The first commercial version of Androi...

How do roosters know the time and why do they crow on time every day?

Apart from the barking of dogs and cats, the crow...

Travel 5 million kilometers across China!

Exceed 5 million km Highway Spreading all over Ch...

Super Sales Private Camp helps you break through performance resistance

Super Sales Private Camp helps you break through ...

Specifically for Omicron! When will this vaccine be available?

A new vaccine targeting the Omicron variant of th...

Watching 300 Douyin videos a day made me rethink Toutiao

Douyin has become a hot commodity. Compared with ...

Qiku phones are waiting for third-party apps to be pre-installed, free of charge

[[135168]] Zhou Hongyi said not long ago that he w...

A simple and crude front-end and back-end separation solution

[[155800]] Project Background I just finished a p...