将 UIView 添加到现有的 UIViewController

Posted

技术标签:

【中文标题】将 UIView 添加到现有的 UIViewController【英文标题】:Adding a UIView to an existing UIViewController 【发布时间】:2015-09-10 16:59:22 【问题描述】:

我正在尝试将控件合并到现有的ViewController 中,但新视图并未绘制到我想要的位置。在下面的代码中,我想我已经找出了我的问题所在,但我不知道如何解决它。

我有一个名为StepperView 的UIView,我正在尝试将其添加到现有的UIViewController 中。过去,当我向UIViewController 添加视图时,我完全以编程方式 完全使用自动布局

在这种情况下,我的情节提要已经在 AutoLayout 中进行了配置,我希望保持这种方式。我无法让 StepperView 显示在我在 Interface Builder 中布局的 UIView 对象中。我在故事板中添加了一个UIView 对象,添加了约束,并在MyViewController.m 中创建了一个IBOutlet。我还为StepperView添加了一条导入语句到MyViewController.m,如下:

#import "StepperView.h"
// a bunch of properties
@property (strong, nonatomic) IBOutlet StepperView *stepperView;

在我的viewDidLoadMyViewController.m 中,我将stepperView 实例化如下:

[self.view addSubview:self.stepperView];
// this is the method I'm calling to instantiate stepperView
[self.stepperView drawStepperInView:self.stepperView];

我想配置StepperView 来填充我在情节提要中设置的UIView 对象。相反,它显示在我在 Interface Builder 中为其设置的屏幕区域之外。

这是来自StepperView.mdrawStepperInView

- (void)drawStepperInView:(UIView *)containerView 

    if (_numberCurrent <= 0) 

        _numberCompleted = 0;
        _numberCurrent = 1;

     else if (_numberCurrent > _numberTotalStep) 

        _numberCompleted = _numberTotalStep - 1;
        _numberCurrent = _numberTotalStep + 1;

     else 
        _numberCompleted = _numberCurrent - 1;
    
    // ** I think my problem lives here...
    CGFloat _startPointX = ([UIScreen mainScreen].bounds.size.width - STEPPER_WIDTH) / 2;
    if (_startX) 
        _startPointX = _startX;
    

    // ** and here.
    CGFloat _startPointY = 100.0f;
    if (_startY) 
        _startPointY = _startY;
    

    _stepperPipeBG = [[UIView alloc] initWithFrame:CGRectMake(_startPointX, _startPointY, STEPPER_WIDTH, STEPPER_HEIGHT)];
    _stepperPipeBG.backgroundColor = [UIColor colorWithRed:(225.0f/256.0f) green:(225.0f/256.0f) blue:(225.0f/256.0f) alpha:1.0f];
    [containerView addSubview:_stepperPipeBG];


    CGFloat _stepperPipeFrontWidth = ((STEPPER_WIDTH / (_numberTotalStep - 1)) * _numberCompleted);
    _stepperPipeFront = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 4.0f, _stepperPipeFrontWidth, (STEPPER_HEIGHT - 8.0f))];
    _stepperPipeFront.backgroundColor = COMPLETED_COLOR;
    _stepperPipeFront.layer.borderWidth = 1.5f;
    _stepperPipeFront.layer.borderColor = COMPLETED_BORDER_COLOR.CGColor;
    [_stepperPipeBG addSubview:_stepperPipeFront];

    for (int i=1; i<=_numberTotalStep; i++) 
        int status = MARK_UNTOUCHED;
        if (i < _numberCurrent) 
            status = MARK_COMPLETED;
         else if (i == _numberCurrent) 
            status = MARK_CURRENT;
         else 
            status = MARK_UNTOUCHED;
        
        [self drawStepperMarkInView:_stepperPipeBG withNumber:i withStatus:status];
    

【问题讨论】:

【参考方案1】:

也许我在这里遗漏了一些东西,但您是否尝试过简单地说:

   stepperView.frame = containerViewFrame

另外,如果您使用的是 AutoLayout,那么您需要覆盖 viewDidLayoutSubviews 方法,以便 AutoLayout 可以首先呈现视图容器。

我会从 viewDidLoad 中删除代码并添加:

 -(void)viewDidLayoutSubviews 
    self.stepperView.frame = self.view.frame;
    [self.view addSubview:self.stepperView];
    [self.stepperView drawStepperInView:self.stepperView];

【讨论】:

你会把那条线放在哪里?【参考方案2】:

MyViewController 上,我将我为stepperView 添加的UIView 对象固定到屏幕的左右边缘。我还将StepperView.mdrawInStepperView:containerView方法中的_startPointY更改为:

CGFloat _startPointY = 0.0f;

【讨论】:

以上是关于将 UIView 添加到现有的 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章

如何将多维数组添加到现有的 Spark DataFrame

如何将 Mailchimp 添加到现有的注册框

将新的 ViewController 嵌入到现有的 UITabBarController 中?

将核心数据添加到现有的 iPhone 项目

如何将新行添加到现有的 QTablewidget

如何将熊猫数据添加到现有的 csv 文件中?