Domanda

I'm trying to position a button so that it is always an X amount of pixels from the bottom of the screen. The view is laid out programmatically using auto layout. If I use: @"V:|-44-[closeModalButton]" the object aligns 44 pixels from the top of the screen as expected. However, @"V:[closeModalButton]-44-|" causes the button to never show up. It seems like it ought to be pretty straightforward, so I feel like I'm missing something really obvious.

Implementation of view

#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

View controller

#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
È stato utile?

Soluzione

Ok, so it looks like DetailView.h subclassed UIScrollView rather than UIView. Setting the proper subclass caused the constraints to operate as expected.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top