Layout and redirection between uiviews in iOS

Source: Internet
Author: User

Layout and redirection between uiviews in iOS

UIView is the base class of all views in iOS development. It represents a rectangular area on the screen and can process the rendering and touch events of this area. UIViewController is the base class of the View Controller. It is used to process switching among screens and provide a view management model. A UIViewController manages a hierarchical UIView. the RootViewController is the first View Controller loaded when the iOS app is started. specifies in storyboard) to display the first interface after the APP is successfully started. therefore, switching between uiviewcontrollers in iOS is particularly important, and it directly determines the switching effect between interfaces of the application. the main jump methods are as follows:

UITabBarController

UITabBarController is mainly used to jump to a horizontal View. The application case is shown in the four tabs at the bottom of the interface. You can select a UIViewController and add it to a Tab Bar In Xcode> Editor> Embed In> Tab bar Controller. you can also directly connect to a View from UITabBarController on the storyboard and select view controllers of Relationship Segue.

Property

Each viewController contained in the tabbar corresponds to a tabbarItem, and the positions are evenly distributed. Up to four tabs are displayed. If more tabs are displayed, the tabs are folded. when setting tabbar through code, you can use setViewControllers to add the specified sub-ViewController as its item.
TabbarItem has the title, image, selectedImage, and badgeValue attributes. badgeValue is the reminder number in the upper right corner of the item, while selectedIndex and selectedViewController locate the currently selected tabbarItem.
In addition, there are viewControllers, selectedViewController, selectedIndex and other attributes, meaning it will not be too long.

UITabBarControllerDelegate

This protocol is used to execute some additional operations when selecting a tabbarItem. Monitoring the tabbar changes can also prevent a tabbarItem from being selected.

UINavigationController stack View Management

UINavigationController is a controller commonly used in IOS development for View switching. It provides a stack-based View management mode. RootViewController is at the bottom of the stack. provides methods for switching and managing views, such
PushViewController and popViewController. For details, refer to the previous summary of UINavigationController.
Generally, the UINavigationController is used to automatically set the title of each View interface, the return button in the upper left corner, and the operation of sliding back to the right of the screen. if you want to disable gesture operations such as sliding back to the right of the screen, you can set the following in the viewDidAppear method of the current View:

Self. navigationController. interactivepopgsturerecognizer. enabled = NO; // disable right-slide and other gestures

Note that UINavigationController uses a stack-like push and pop method to switch the view. The call method is pushViewController and popViewController, And the segger attribute must be set to push.
You can use the viewControllers attribute to obtain the current view stack.

Property

ToolbarHidden is used to hide the navigation toolbar at the top of navigationController. In This toolbar, we can add various Bar Button Item controls by ourselves. Commonly Used Bar buttons include leftBarButtonItem and rightBarButtonItem.
UINavigationItem is each item in the View stack. You can specify it in the Storyboard or xib file, or create your own code and add it to UINavigationController.

UINavigationControllerDelegate

This Protocol provides a lot of traversal methods for View redirection in NavigationController. for example, didShowViewController, willShowViewController, and animationControllerForOperation. the enumeration UINavigationControllerOperation defines the jump mode between views (None, Push, Pop ).

Use a nib file

The nib file is a combination of a series of uiviews.

NSArray *arrayMessage = [[NSBundle mainBundle] loadNibNamed:@“ViewMessageCenter” owner:nil options:nil];self.vMessageCenter = [arrayMessage objectAtIndex:0];self.vMessageCenter.navigationController = self.navigationController;self.vMessageCenter.frame = self.vMainPanel.bounds;
Use storyboard

Place a ViewController In the storyboard and call instantiateViewControllerWithIdentifier to load the storyboard (a set of views) with the corresponding ID in the storyboard file. This is also a very common method.

UIStoryboard *sb = [UIStoryboard storyboardWithName:@“Main” bundle:nil];LoginViewController *vc = [sb instantiateViewControllerWithIdentifier:@“LoginViewController”];[self.navigationController popToRootViewControllerAnimated: YES];[self.navigationController presentViewController: vc animated: YES completion:nil];
Use a nib file

A Nib file is a special type of resource file. It stores the Interface Builder document and can be edited visually.
Each xib file corresponds to a ViewController or a custom View. You can use the loadNibNamed: Method to load the nib file.

NSArray *arrayMessage = [[NSBundle mainBundle] loadNibNamed:@“ViewMessageCenter” owner:nil options:nil];self.vMessageCenter = [arrayMessage objectAtIndex:0];self.vMessageCenter.navigationController = self.navigationController;self.vMessageCenter.frame = self.vMainPanel.bounds;
Segue

For two independent viewcontrollers, you can specify the jump mode using segue.
For example, in the storyboard, right-click the button in VC1 and connect it to the second VC. Select the jump mode to achieve mutual jump between the two VC.
What if you want to achieve VC jump by clicking an image? This requires the introduction of gesture.

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

Set the gesture when loading the View:

self.imageView.userInteractionEnabled = YES;UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped)];[self.imageView addGestureRecognizer:imageTap];
Use addSubView

For example, self. view. addSubView (newView) can directly load the UIView and use removeFromSuperview to remove the UIView.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.