简体   繁体   中英

Hardcoded UIView vs. XIB UIView resizing

I'm stumbled on how to layout a UITextField when there is a hardcoded UIImageView . UIImageView is hardcoded, let me show it:

//  Image
CGFloat xCenter = self.view.frame.size.width / 2.00;
CGFloat yCenter = self.view.frame.size.height / 2.00;



if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    self.imageFrame = CGRectMake(xCenter + 115, yCenter - 425, 250, 250);
}

else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    self.imageFrame = CGRectMake(xCenter - 125 , yCenter, 250, 250);
}

if ([[BNRImageStore sharedStore] imageForKey:self.item.itemKey]) {
    self.imageView = [[UIImageView alloc] initWithFrame:self.imageFrame];
    self.imageView.image = [[BNRImageStore sharedStore] imageForKey:self.item.itemKey];
    [self.imageView.layer setBorderColor:(__bridge CGColorRef)([UIColor blackColor])];
    [self.imageView.layer setBorderWidth:3.0];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:self.imageView];

    NSDictionary *nameMap =@{@"imageView": self.imageView,
                             @"nameLabel": self.nameLabel,
                             @"nameField": self.nameField};

    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[nameLabel]-[nameField]-[imageView(==250)]-|" options:0 metrics:nil views:nameMap];

    [self.view addConstraints:@[horizontalConstraints]];
}

else {
    UIView *imageCanvasView = [[UIView alloc] initWithFrame:self.imageFrame];

    imageCanvasView.backgroundColor = [UIColor colorWithWhite:1.00 alpha:0.4];

    imageCanvasView.layer.borderWidth = 1.0f;
    imageCanvasView.layer.borderColor = [UIColor colorWithWhite:0.00 alpha:0.9].CGColor;
    [self.view addSubview:imageCanvasView];

    UILabel *addImageLabel = [[UILabel alloc] initWithFrame:self.imageFrame];
    addImageLabel.text = @"(tap to add image)";
    addImageLabel.font = [UIFont fontWithName:@"AvenirNext-UltraLight"
                                         size:20];
    addImageLabel.textColor = [UIColor WHITE_COLOR_DEBUG_FIX];
    addImageLabel.textAlignment = NSTextAlignmentCenter;
    addImageLabel.hidden = NO;

    [self.view addSubview:addImageLabel];
}

Seems it's what I need: 苹果手机

But not in iPad: 的iPad

Except UIImageView *imageView property, I made the constants with Auto Layout. Since XIB file doesn't recognize imageView I couldn't set the constraints of it. Instead I can use VFL, but constraints intersect at some point. Thus I couldn't managed it out.

Any ideas? (or is it possible in some way?)

I had a similar problem, I resolved it by creating two holder UIViews either on storyboard or programmatically - one holding the form and one holding the hardcoded UIImageView.

If you're using storyboards : Set the constraints on those two holding views and in the subviews of the one containing the form. And just place your UIImageView programmatically in the center of your image-holding view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM