简体   繁体   中英

Adding multiple subviews to a UIView

I'm using an iOS charting software called TWRCharts, and I'm trying to add multiple charts in the same view controller. I have added UIViews using Interface Builder, and connected them using outlets in my .h file. When I try showing one chart, my main chart, it shows fine. But when I try adding more charts, they don't appear. I have tried reversing the order of chart drawing, and in this case one of the smaller charts appears, but nothing else. When I try adding multiple subviews to their respective views, nothing appears. Here are my .h and .m files.

.h

#import <UIKit/UIKit.h>
#import "TWRChart.h"

@interface OverviewViewController : UIViewController

- (IBAction)didTapButton:(id)sender;

@property (strong, nonatomic) IBOutlet UIView *mainCircularChartUIView;
@property (strong, nonatomic) TWRChartView *mainCircularChart;

@property (strong, nonatomic) IBOutlet UIView *tapTestCircularChartUIView;
@property (strong, nonatomic) TWRChartView *tapTestCircularChart;

@property (strong, nonatomic) IBOutlet UIView *cogTestCircularChartUIView;
@property (strong, nonatomic) TWRChartView *cogTestCircularChart;

@property (strong, nonatomic) IBOutlet UIView *voiceTestCircularChartUIView;
@property (strong, nonatomic) TWRChartView *voiceTestCircularChart;

@property (strong, nonatomic) IBOutlet UIView *mentalHealthTestControllerUIView;
@property (strong, nonatomic) TWRChartView *mentalHealthTestController;

@end

.m (currently commenting out adding more than one subview)

- (void)viewDidLoad {
[super viewDidLoad];

/////////// Main Circular Chart View ////////////////////////
self.mainCircularChart = [[TWRChartView alloc] initWithFrame:CGRectMake(0, 60, 300, 300)];
self.mainCircularChart.backgroundColor = [UIColor clearColor];
self.mainCircularChart.userInteractionEnabled = YES;

// Add the chart view to the controller's view
[self.mainCircularChartUIView addSubview:self.mainCircularChart];

/////////// Sub Circular Chart Views ////////////////////////
self.tapTestCircularChart = [[TWRChartView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
self.tapTestCircularChart.backgroundColor = [UIColor clearColor];
self.tapTestCircularChart.userInteractionEnabled = YES;

self.cogTestCircularChart = [[TWRChartView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
self.cogTestCircularChart.backgroundColor = [UIColor clearColor];
self.cogTestCircularChart.userInteractionEnabled = YES;

// Add the chart view to the controller's view
//    [self.tapTestCircularChartUIView addSubview:self.tapTestCircularChart];
//    [self.cogTestCircularChartUIView addSubview:self.cogTestCircularChart];

[self loadMainCircularChart];
[self loadSubCircularCharts];
}

- (void)loadSubCircularCharts {
// Values
NSArray *values = @[@20, @30, @15, @5];

// Colors
UIColor *color1 = [UIColor colorWithHue:0.5 saturation:0.6 brightness:0.6 alpha:1.0];
UIColor *color2 = [UIColor colorWithHue:0.6 saturation:0.6 brightness:0.6 alpha:1.0];
UIColor *color3 = [UIColor colorWithHue:0.7 saturation:0.6 brightness:0.6 alpha:1.0];
UIColor *color4 = [UIColor colorWithHue:0.8 saturation:0.6 brightness:0.6 alpha:1.0];
NSArray *colors = @[color1, color2, color3, color4];


// Tap Test Chart ///////////////////////////////////////////
TWRCircularChart *tapTestChart = [[TWRCircularChart alloc] initWithValues:values colors:colors type:TWRCircularChartTypeDoughnut animated:YES];

[self.tapTestCircularChart loadCircularChart:tapTestChart];

// Cog Test Chart ///////////////////////////////////////////
TWRCircularChart *cogTestChart = [[TWRCircularChart alloc] initWithValues:values colors:colors type:TWRCircularChartTypeDoughnut animated:YES];
//
[self.tapTestCircularChart loadCircularChart:cogTestChart];

}

- (void)loadMainCircularChart {
// Values
NSArray *values = @[@20, @30, @15, @5];

// Colors
UIColor *color1 = [UIColor colorWithHue:0.5 saturation:0.6 brightness:0.6 alpha:1.0];
UIColor *color2 = [UIColor colorWithHue:0.6 saturation:0.6 brightness:0.6 alpha:1.0];
UIColor *color3 = [UIColor colorWithHue:0.7 saturation:0.6 brightness:0.6 alpha:1.0];
UIColor *color4 = [UIColor colorWithHue:0.8 saturation:0.6 brightness:0.6 alpha:1.0];
NSArray *colors = @[color1, color2, color3, color4];

// Doughnut Chart
TWRCircularChart *pieChart = [[TWRCircularChart alloc] initWithValues:values colors:colors type:TWRCircularChartTypeDoughnut animated:YES];

[self.mainCircularChart loadCircularChart:pieChart];

}

You can have two ways to implement

  1. By using scroll view

if you have problem in using scroll view then

  1. you can achieve it by using UITableView by using custom cells in it.

Have you added a UIScrollView as main subview of your VC? Without a scrollview, your content won't scroll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM