【问题标题】:How to initialize and add a subview to a custom UIView?如何初始化子视图并将其添加到自定义 UIView?
【发布时间】:2017-08-12 09:45:00
【问题描述】:

我有一个继承自 UIView 的自定义视图。现在我想添加一个 UISegmentedControl 作为子视图。 那么第一个问题是:UISegmentedControl 的属性是弱还是强? (对于 IBOutlets,我知道 Apple 自 2015 年以来建议使用 strong)。 第二个问题是我在哪里初始化它并设置它的布局。据我了解,我不应该在 drawRect: 方法中这样做。是否应该在 initWithFrame: 方法中进行初始化,将其作为子视图添加到我的自定义视图中,然后在 layoutSubviews 中设置其布局,如下所示:

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        NSArray *options = @[@"option1", @"option2", @"option3"];
        _segmentedControl = [[UISegmentedControl alloc] initWithItems:options];
        [_segmentedControl addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventValueChanged];

        [self addSubview:_segmentedControl];
    }

    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect segmentedControlFrame = CGRectMake(self.bounds.size.width / 4.0, 50, self.bounds.size.width / 2.0, 30);
    self.segmentedControl.frame = segmentedControlFrame;
    self.segmentedControl.tintColor = [UIColor blackColor];
    [self.segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateSelected];
}

或者只是在 layoutSubviews: 方法中完成所有这些操作:

- (void)layoutSubviews {
    NSArray *options = @[@"option1", @"option2", @"option3"];
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:options];

    CGRect segmentedControlFrame = CGRectMake(self.bounds.size.width / 4.0, 50, self.bounds.size.width / 2.0, 30);
    segmentedControl.frame = segmentedControlFrame;
    segmentedControl.tintColor = [UIColor blackColor];
    [segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateSelected];
    [segmentedControl addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventValueChanged];

    [self addSubview:segmentedControl];
}

【问题讨论】:

    标签: ios objective-c uiview


    【解决方案1】:

    如果您的视图将保留分段控件(如果它不包含在其层次结构中),则您应该使用strong。当分段控件始终是层次结构的一部分时,您可以使用weak,但在将其添加到层次结构之前,您必须保持它具有强引用。在您的第一个代码 sn-p 中,您必须使用一个本地(强)变量来保存控制:

    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
    
        if (self) {
            NSArray *options = @[@"option1", @"option2", @"option3"];
            UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:options];
    
            [control addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventValueChanged];
    
            [self addSubview:control]; // By now the view keeps the control
            self.segmentedControl = control;
        }
        return self;
    }
    

    弱引用的优点是,如果您从层次结构中删除控件,它会自动重置。这可以帮助您保持较小的内存量。

    对于布局,您应该更喜欢自动布局约束。自动调整大小也是可能的(无论如何,请查看translatesAutoresizingMaskInotConstraints)。 layoutSubviews 不应该将段添加到视图中,因为它被多次调用。

    【讨论】:

    • 谢谢!例如,如果我有一个 UIViewController,在它的 loadView: 方法中我将我的自定义视图分配给视图控制器的视图(self.view=customView)。我决定在这个视图控制器中添加 UISegmentedControl 作为子视图,而不是在自定义视图中。我应该在哪里做?在 viewDidLoad: 方法中?那么在方法中创建一个 UISegmentedControl 作为局部变量然后将其添加为子视图呢?它比将其创建为财产更好吗? (但是在其他方法中如果我想访问它,我需要通过 self.view.subviews 来完成)。
    • 一般viewDidLoad 是一个非常好的添加视图的地方。当您想稍后访问视图时,您只需要属性(例如:viewWillAppear:,操作方法)。遍历层次结构是可能的,但并不有趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多