我强烈建议使用约束而不是框架。它会完全按照您的要求进行,并且会容易得多。您的代码可能看起来像
[self.superViewOfButton addSubview:self.button1];
[self.superViewOfButton addSubview:self.button2];
[self.superViewOfButton addSubview:self.button3];
NSDictionary *viewsDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:self.button1, self.button2, self.button3, nil]
forKeys:[NSArray arrayWithObjects:@"button1", @"button2", @"button3", nil]];
[self.superViewOfButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[button1]" options:0 metrics:0 views:viewsDic]];
[self.superViewOfButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[button1]" options:0 metrics:0 views:viewsDic]];
[self.superViewOfButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[button2]" options:0 metrics:0 views:viewsDic]];
[self.superViewOfButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[button2]" options:0 metrics:0 views:viewsDic]];
这是视觉语法语言的链接:http:
//developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/formatLanguage.html
并记住将所有控件设置为
button1.translatesAutoresizingMaskIntoConstraints = NO;
起初它可能看起来有点多,但在阅读大约五分钟后,它非常简单,并且使您的应用程序更加灵活。希望这可以帮助。