【问题标题】:iOS bottom align an object with programmatic auto layout constraintsiOS底部对齐具有编程自动布局约束的对象
【发布时间】:2014-04-22 15:04:03
【问题描述】:

我正在尝试定位一个按钮,使其始终距离屏幕底部 X 像素。视图使用自动布局以编程方式布局。如果我使用:@"V:|-44-[closeModalButton]",该对象按预期从屏幕顶部对齐 44 个像素。但是,@"V:[closeModalButton]-44-|" 导致按钮永远不会出现。看起来它应该很简单,所以我觉得我错过了一些非常明显的东西。

视图的实现

#import "DetailView.h"

@implementation DetailView

@synthesize closeModalButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor whiteColor];
        CGRect aFrame = [[UIScreen mainScreen] bounds];
        self.frame = aFrame;

        closeModalButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [closeModalButton setImage: [UIImage imageNamed:@"closeButton.png"] forState:UIControlStateNormal];
        [closeModalButton setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addSubview:closeModalButton];

        NSDictionary *viewArranging = NSDictionaryOfVariableBindings(closeModalButton);

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[closeModalButton(44)]-44-|"
                                                                     options:0
                                                                     metrics:0
                                                                       views:viewArranging]];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-149-[closeModalButton(44)]-149-|"
                                                                     options:0
                                                                     metrics:0
                                                                       views:viewArranging]];
    }
    return self;
}

@end

视图控制器

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
{
    DetailView *_detailView;
}

- (void)loadView
{    
    _detailView = [[DetailView alloc] init];
    [self setView:_detailView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [_detailView.closeModalButton addTarget:self
                                     action:@selector(closeModalButtonPressed:)
                           forControlEvents:UIControlEventTouchUpInside];

}

- (void)closeModalButtonPressed:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES
                             completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

  • 由于按钮在与视图顶部对齐时有效,但在与底部对齐时无效,视图是否可能以某种方式没有设置底部边界?
  • 它很可能出现在它应该出现的地方,并且你的 superView 比屏幕大。您可以在布局后通过 NSLoging 帧检查这一点。
  • 我复制了你的代码,它对我来说效果很好(除了水平约束的警告(它应该只有一侧的约束,或 centerX 约束)。
  • @rdelmar 嗯,这真的很奇怪。知道什么可能导致底部约束不适用于我吗?此外,我的编译器不会抛出水平约束警告。数学似乎完全正确。
  • @RyanPoolos NSLog(@"%f", self.view.frame.size.height); viewDidLoad 返回 568 后

标签: ios objective-c constraints autolayout visual-format-language


【解决方案1】:

好的,看起来 DetailView.h 是 UIScrollView 的子类,而不是 UIView。设置正确的子类会导致约束按预期运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多