-1

我有一个 ViewController,它添加一个UIView,SpeciesImageView作为子视图viewDidLoad并在viewWillLayoutSubviews.

SpeciesImageView没有 nib 文件。当我们在 viewDidLoad 中创建时,它会在类中speciesImageView调用。initWithFrameSpeciesImageView

这工作正常(横向和纵向),直到手机旋转。我尝试将约束设置为speciesImageView.frame.size.width,但这不起作用,因为initWithFrame在方向更改时不会调用,因此高度/宽度speciesImageView保持不变。

另一方面, usingscreenRect不会改变 的实际大小UIView,它会改变它在超级视图中的大小。所以换句话说,我还没有找到改变实际speciesImageView方向变化大小的方法。

而且由于我丢失的原因,当您将其旋转回原始位置时,它会完全混乱。

- (void)viewDidLoad
{

    self.tabBarController.tabBar.hidden=YES;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.hidden = NO;
    //self.navigationController.navigationBar.translucent = YES;


    UIImage *plantinfo;
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        plantinfo = [UIImage imageNamed:@"plantinfo_frame.png"];
    } else {
        plantinfo = [UIImage imageNamed:@"plantinfo.png"];
    }

    UIBarButtonItem *tempButton = [[UIBarButtonItem alloc] initWithImage:plantinfo
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self
                                                                  action:@selector(toggleText:)];
    self.navigationItem.rightBarButtonItem = tempButton;
    [tempButton release];
    self.title = theSpecies.scientificName;
    //[self.navigationItem.backBarButtonItem setTitle:@""];
    self.navigationItem.backBarButtonItem.title = @"";


    infoViewSegmentedControl.backgroundColor = [UIColor blackColor];
    webView.backgroundColor = [UIColor blackColor];
    _activityIndicator.hidden = YES;

    [webView setOpaque:YES];
    webView.delegate = self;
    // Do double justification
    [webView loadHTMLString:[self formatHTML:theSpecies] baseURL:nil];
    showingInfoView = NO;

    //
    //  Resize containerView, infoview according to iphone 5 screen size.
    //

    infoView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
    CGPoint screenOrigin = [[UIScreen mainScreen] bounds].origin;
    CGSize viewSize = [[UIScreen mainScreen] bounds].size;
    CGPoint origin = infoView.frame.origin;

    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        infoView.frame = CGRectMake(screenOrigin.x,
                                    screenOrigin.y + statusBarFrame.size.height,
                                    viewSize.width,
                                    viewSize.height - origin.y - statusBarFrame.size.height);

        speciesImageView = [[SpeciesImageView alloc]
                            initWithFrame:CGRectMake(screenOrigin.x,
                                                     screenOrigin.y,
                                                     viewSize.width,
                                                     viewSize.height)];
    } else {
        infoView.frame = CGRectMake(screenOrigin.x,
                                    screenOrigin.y,
                                    viewSize.width,
                                    viewSize.height - origin.y - statusBarFrame.size.height);

        speciesImageView = [[SpeciesImageView alloc]
                            initWithFrame:CGRectMake(screenOrigin.x,
                                                     screenOrigin.y,
                                                     viewSize.width,
                                                     viewSize.height - statusBarFrame.size.height)];
    }

    speciesImageView.delegate = self;
    [containerView addSubview:speciesImageView];

    managedObjectContext = [(LeafletAppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
    [self parseImageURLArray];
}

-(void)viewWillLayoutSubviews{
    if(speciesImageView.window != nil){
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        speciesImageView.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint *widthConst = [NSLayoutConstraint
                                          constraintWithItem:speciesImageView
                                          attribute:NSLayoutAttributeWidth
                                          relatedBy:NSLayoutRelationEqual
                                          toItem:nil
                                          attribute:NSLayoutAttributeNotAnAttribute
                                          multiplier:1.0
                                          constant:screenRect.size.width];

        NSLayoutConstraint *heightConst = [NSLayoutConstraint
                                           constraintWithItem:speciesImageView
                                           attribute:NSLayoutAttributeHeight
                                           relatedBy:NSLayoutRelationEqual
                                           toItem:nil
                                           attribute:NSLayoutAttributeNotAnAttribute
                                           multiplier:1.0
                                           constant:screenRect.size.height];

        NSLayoutConstraint *rightConstraint = [NSLayoutConstraint
                                               constraintWithItem:speciesImageView
                                               attribute:NSLayoutAttributeCenterX
                                               relatedBy:NSLayoutRelationEqual
                                               toItem:self.view
                                               attribute:NSLayoutAttributeCenterX
                                               multiplier:1.0
                                               constant:0.0];


        NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint
                                                constraintWithItem:speciesImageView
                                                attribute:NSLayoutAttributeBottom
                                                relatedBy:NSLayoutRelationEqual
                                                toItem:self.view
                                                attribute:NSLayoutAttributeBottom
                                                multiplier:1.0
                                                constant:0.0];
        [self.view addConstraints:@[widthConst, heightConst, bottomConstraint, rightConstraint]];
    }
}

- (id)initWithFrame:(CGRect)frame
{

    if (self = [super initWithFrame:frame])
    {
        imageScrollView = [[UIScrollView alloc] initWithFrame:frame];
        imageScrollView.delegate = self;
        imageScrollView.backgroundColor = [UIColor blackColor];
        [self addSubview:imageScrollView];

        imageScrollView.translatesAutoresizingMaskIntoConstraints = NO;


        NSLayoutConstraint *widthConst = [NSLayoutConstraint constraintWithItem:imageScrollView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:imageScrollView.frame.size.width];

        NSLayoutConstraint *heightConst = [NSLayoutConstraint constraintWithItem:imageScrollView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:imageScrollView.frame.size.height];


        NSLayoutConstraint *rightConstraint = [NSLayoutConstraint
                                               constraintWithItem:imageScrollView
                                               attribute:NSLayoutAttributeRight
                                               relatedBy:NSLayoutRelationEqual
                                               toItem:self
                                               attribute:NSLayoutAttributeRight
                                               multiplier:1.0
                                               constant:0.0];


        NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint
                                                constraintWithItem:imageScrollView
                                                attribute:NSLayoutAttributeTop
                                                relatedBy:NSLayoutRelationEqual
                                                toItem:self
                                                attribute:NSLayoutAttributeTop
                                                multiplier:1.0
                                                constant:0.0];
        [self addConstraints:@[widthConst, heightConst, bottomConstraint, rightConstraint]];
    }
    return self;
}

进入 旋转一次 再转回来

4

1 回答 1

0

如果您希望您的视图调整到超级视图的大小,那么您需要这样的东西(将边距设置为您喜欢的任何值):

CGFloat margin = 0;

NSString * visualFormatH = [NSString stringWithFormat:@"|-(%f)-[speciesImageView]-(%f)-|", margin, margin];
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:visualFormatH
                                                                           options:0
                                                                           metrics:Nil
                                                                             views:@{@"speciesImageView": speciesImageView}]];
NSString * visualFormatV = [NSString stringWithFormat:@"V:|-(%f)-[speciesImageView]-(%f)-|", margin, margin];
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:visualFormatV
                                                                           options:0
                                                                           metrics:Nil
                                                                             views:@{@"speciesImageView": speciesImageView}]];

现在,speciesImageView 将在 superview 框架发生变化时调整其框架。

这是一个通用示例:

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

UIView * sampleView = [UIView new];
sampleView.backgroundColor = [UIColor redColor];
sampleView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:sampleView];


CGFloat margin = 0;

NSString * visualFormatH = [NSString stringWithFormat:@"|-(%f)-[sampleView]-(%f)-|", margin, margin];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:visualFormatH
                                                                       options:0
                                                                       metrics:Nil
                                                                         views:@{@"sampleView": sampleView}]];
NSString * visualFormatV = [NSString stringWithFormat:@"V:|-(%f)-[sampleView]-(%f)-|", margin, margin];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:visualFormatV
                                                                       options:0
                                                                       metrics:Nil
                                                                         views:@{@"sampleView": sampleView}]];


 }

自动布局入门

自动布局视觉格式文档

于 2018-04-21T08:06:41.460 回答