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

Scientific rumor-busting | The chemistry in fireworks and firecrackers

General Secretary Xi Jinping pointed out: "S...

In addition to Meizu, there will be more Ubuntu phones on the market

At the moment, it's hard to measure the succe...

Advertising your own products is the correct way to promote your products!

When it comes to promotion , most people think of...

These Android apps may stop working in 2022

ARM held the DevSummit Developer Summit. At the m...

10w+ new followers in 4 days, how to increase followers by splitting?

Today I want to talk to you about the entire clos...

Bong bracelet price cut reflects the "alchemy" of wearable devices

Since the price of Xiaomi Mi Band was announced a...

How to make guidance no longer useless and transparent?

When using a new app, users often see some guide ...

How does Zuoyebang operate private domain traffic?

For the education industry, 2020 is definitely a ...

Review: Baidu information flow delivery on social apps

Recently, I took over a Baidu information flow ac...

A review of the top 10 Douyin e-commerce cases of the year!

Douyin e-commerce in 2021 is like the American We...