【发布时间】:2015-06-19 02:04:35
【问题描述】:
我有一个UIView 子类,它将是一个工具提示,可以出现在任何视图控制器的底部,以显示提示/错误消息/成功消息/等。工具提示视图有两个子视图:左侧的 UILabel 和右侧的 UIView,可以是任何东西(但通常用作 UIButton)。
TooltipView.h
@interface TooltipView : UIView
@property (nonatomic, readonly) UILabel *textLabel;
@property (nonatomic) UIView *rightView;
TooltipView.m
static CGFloat const kSubviewToSuperviewSpacing = 7.0f;
static CGFloat const kTextLabelToRightViewHorizontalSpacing = 7.0f;
@implementation TooltipView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_textLabel = [[UILabel alloc] init];
_textLabel.numberOfLines = 0;
[_textLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:_textLabel];
}
return self;
}
- (void)setRightView:(UIView *)rightView {
if (_rightView != rightView) {
_rightView = rightView;
[self addSubview:_rightView];
[self setNeedsDisplay];
}
}
- (BOOL)translatesAutoresizingMaskIntoConstraints {
return NO;
}
- (void)updateConstraints {
self.didUpdateConstraints = YES;
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.textLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:kSubviewToSuperviewSpacing]];
NSMutableDictionary *metricsDictionary = [@{@"subviewToSuperviewSpacing": @(kSubviewToSuperviewSpacing)} mutableCopy];
NSMutableDictionary *viewsDictionary = [@{@"textLabel": self.textLabel} mutableCopy];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-subviewToSuperviewSpacing-[textLabel]-subviewToSuperviewSpacing-|"
options:0
metrics:metricsDictionary
views:viewsDictionary]];
if (self.rightView) {
metricsDictionary[@"textLabelToRightViewHorizontalSpacing"] = @(kTextLabelToRightViewHorizontalSpacing);
viewsDictionary[@"rightView"] = self.rightView;
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-subviewToSuperviewSpacing-[rightView]-subviewToSuperviewSpacing-|"
options:0
metrics:metricsDictionary
views:viewsDictionary]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:kSubviewToSuperviewSpacing]];
}
[super updateConstraints];
}
我也有一个视图控制器来测试这个UILabel 作为rightView。它是UIViewController 的子类,它唯一的方法是覆盖loadView:
- (void)loadView {
TooltipView *tooltipView = [[TooltipView alloc] initWithFrame:CGRectZero];
tooltipView.textLabel.text = @"Test 1";
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectZero];
testLabel.text = @"Hello there";
[testLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
tooltipView.rightView = testLabel;
self.view = tooltipView;
}
最后,在我想要显示工具提示的视图控制器中,我将 TooltipViewController 添加为子视图控制器。最终,这将由某些事件触发,但出于测试目的,我将其添加到 viewWillAppear:。
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.aViewController = [[TooltipViewController alloc] init];
self.aViewController.view.backgroundColor = [UIColor whiteColor];
[self addChildViewController:self.aViewController];
[self.view addSubview:self.aViewController.view];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|"
options:0
metrics:nil
views:@{@"view": self.aViewController.view}]];
[self.aViewController.view addConstraint:[NSLayoutConstraint constraintWithItem:self.aViewController.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0f
constant:150.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.aViewController.view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0f]];
[self.aViewController didMoveToParentViewController:self];
}
但是,在运行时,textLabel 和rightView 的框架似乎设置正确,但tooltipView 的框架设置为CGRectZero,三个视图都没有出现在屏幕上。
提前致谢!
编辑
我什至尝试完全删除textLabel 和rightView,所以只是一个带有前导、尾随、底部和高度约束的普通 UIView。没有什么是模棱两可的,但视图的框架仍然是CGRectZero。
【问题讨论】:
-
您是否在调试器窗口中没有收到一些警告,例如“无法同时满足约束......”和“......将在打破约束后尝试渲染......”?如果有,请确认。
-
@Gandalf 不,我已经检查以确保布局没有歧义,并且布局没有过度约束(布局系统没有记录任何内容)。
-
感谢@matt 的反馈,非常感谢!这个想法是为应用程序中的任何视图控制器提供一个框架,以便在需要时显示工具提示,并且工具提示可以根据情况以各种方式进行动画处理(可以淡入、可以滑入、可以淡出、可以滑出) .使用视图控制器和 UIViewControllerAnimatedTransitioning 似乎是执行此操作的合乎逻辑的方式,但如果您不同意,请告诉我!
-
@matt 我喜欢这里使用的模式:objc.io/issues/12-animations/…。对我来说,动画封装的好处超过了使用视图控制器的“重量级”(在这种情况下,我不认为它是重量级的,除非你能证明有显着的性能影响)。
标签: ios objective-c uiview uiviewcontroller autolayout