Starting with iOS8, Xcode can create Framework projects directly (👍), no longer need to create static libraries and then modify them to the Framework. Additionally, using the Framework in iOS7 is unfriendly to Xib and image references, and iOS8 provides a direct API.

First, create Framwork project

To create a new project, select Framework & Library -> Cocoa Touch Framework and click Next to create a Framework project for FrameworkiOS8. Add the class and Xib of the FrameworkViewController, and add the Bundle of the image. See github Demo for details.

Header file is exposed to the TARGETS – > Build Phases – > Headers will FrameworkViewController. H from the Project to the Public.

In FrameworkViewController. H exposes a method in the – (void) displayImageView; Used to display images in FrameworkViewController. M

#pragma mark - Public Methods

- (void)displayImageView
{
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    
    UIImage *image = [UIImage imageNamed:@"Images.bundle/LuFei" inBundle:bundle compatibleWithTraitCollection:nil];
    
    self.imageView.image = image;
}
Copy the code

Loads the PNG file in the specified bundle.

Create a test project

Create a project to test the Framework:

  1. Create a FrameworkTest project and copy the FrameworkiOS8. Framework file generated by compiling FramworkiOS8 into the FrameworkTest directory.

  2. FrameworkTest project introduces FrameworkiOS8. Framework.

  3. Under Targets -> General -> Embedded Binaries introduce the Framework

  4. Introduce the framework in Build Phases -> Copy Bundle Resources.

  5. Write call Framework of test code, introducing the # import < FrameworkiOS8 / FrameworkiOS8. H > header file.

  6. Execute test code

#pragma mark - Target Methods

- (IBAction)onClickFrameworkBtn:(id)sender
{
    NSBundle *bundle = [NSBundle bundleForClass:[FrameworkViewController class]];
    
    FrameworkViewController *frameworkVC = [[FrameworkViewController alloc] initWithNibName:@"FrameworkViewController" bundle:bundle];
    
    [self presentViewController:frameworkVC animated:YES completion:^{
        [frameworkVC displayImageView];
    }];
}
Copy the code
  1. The Xib in the Framework is displayed correctly. 👍 ^_^

Directly load images directly load images in the Framework

- (IBAction)onClickImageBtn:(id)sender
{
    NSBundle *bundle = [NSBundle bundleForClass:[FrameworkViewController class]];
    
    UIImage *image = [UIImage imageNamed:@"Images.bundle/LuFei" inBundle:bundle compatibleWithTraitCollection:nil];
    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:image]];
}
Copy the code

Be successful.

// END

For Framework packaging, online updates, and so on, please refer to other literature. Do not repeat the description.