我用6个按钮创建了一个视图,问题是当我旋转到横向时,图形效果不佳。
应该是这样的:loghos.eu/hoch.png loghos.eu/quer.png
经过一番研究后,我看到了这个脚本:Link
我试图通过iPhone的分辨率来管理它。 但它不起作用
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
#define IS_IPHONE_5 ([[UIScreen mainScreen] bounds].size.height == 568)
#define IS_IPHONE_6_Plus ([[UIScreen mainScreen] bounds].size.height == 736)
if( IS_IPHONE_6_Plus )
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
eins.frame = CGRectMake(40, 75, 115, 115);
zwei.frame = CGRectMake(530, 75, 115, 115);
drei.frame = CGRectMake(290, 75, 115, 115);
vier.frame = CGRectMake(290, 230, 115, 115);
fuenf.frame = CGRectMake(40, 230, 115, 115);
sechs.frame = CGRectMake(530, 230, 115, 115);
}
else
{
eins.frame = CGRectMake(30, 95, 115, 115);
zwei.frame = CGRectMake(250, 95, 115, 115);
drei.frame = CGRectMake(30, 300, 115, 115);
vier.frame = CGRectMake(250, 300, 115, 115);
fuenf.frame = CGRectMake(30, 530, 115, 115);
sechs.frame = CGRectMake(250, 530, 115, 115);
}
}
if( IS_IPHONE_5 )
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
eins.frame = CGRectMake(40, 75, 115, 115);
zwei.frame = CGRectMake(530, 75, 115, 115);
drei.frame = CGRectMake(290, 75, 115, 115);
vier.frame = CGRectMake(290, 230, 115, 115);
fuenf.frame = CGRectMake(40, 230, 115, 115);
sechs.frame = CGRectMake(530, 230, 115, 115);
}
else
{
eins.frame = CGRectMake(30, 95, 115, 115);
zwei.frame = CGRectMake(190, 95, 115, 115);
drei.frame = CGRectMake(30, 220, 115, 115);
vier.frame = CGRectMake(190, 220, 115, 115);
fuenf.frame = CGRectMake(30, 330, 115, 115);
sechs.frame = CGRectMake(190, 330, 115, 115);
}
}
}
答案 0 :(得分:0)
基于设备硬编码这样的按钮帧是非常不好的做法。相反,你的目标应该是通过自动布局或你自己的数学创建一个适应任何大小的界面。
我建议您使用viewDidLayoutSubviews
正确布局视图。由于必须再次布局视图,因此在发生旋转时会自动调用此方法。从那里,如果您使用的是iOS 8,则可以检测视图的大小类,或者回退到检测iOS 7上的界面方向。
以下是viewDidLayoutSubviews
方法的示例:
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// first decide whether we are landscape
BOOL isLandscape = NO;
if ([self respondsToSelector:@selector(traitCollection)]) {
if ([[self traitCollection] verticalSizeClass] == UIUserInterfaceSizeClassCompact) {
isLandscape = YES;
}
} else {
if ([self interfaceOrientation] == UIDeviceOrientationLandscapeLeft || [self interfaceOrientation] == UIDeviceOrientationLandscapeRight) {
isLandscape = YES;
}
}
// now layout the views depending on if it is landscape or not
CGFloat const buttonSize = 115;
if (isLandscape) {
CGFloat const columnWidth = CGRectGetWidth([[self view] bounds]) / 3.0;
CGFloat const leftColumnX = (columnWidth - buttonSize) / 2.0;
CGFloat const middleColumnX = columnWidth + leftColumnX;
CGFloat const rightColumnX = columnWidth * 2 + leftColumnX;
CGFloat const rowHeight = CGRectGetHeight([[self view] bounds]) / 2.0;
CGFloat const topRowY = (rowHeight - buttonSize) / 2.0;
CGFloat const bottomRowY = rowHeight + topRowY;
[[self viewOne] setFrame:CGRectMake(leftColumnX, topRowY, buttonSize, buttonSize)];
[[self viewTwo] setFrame:CGRectMake(middleColumnX, topRowY, buttonSize, buttonSize)];
[[self viewThree] setFrame:CGRectMake(rightColumnX, topRowY, buttonSize, buttonSize)];
[[self viewFour] setFrame:CGRectMake(leftColumnX, bottomRowY, buttonSize, buttonSize)];
[[self viewFive] setFrame:CGRectMake(middleColumnX, bottomRowY, buttonSize, buttonSize)];
[[self viewSix] setFrame:CGRectMake(rightColumnX, bottomRowY, buttonSize, buttonSize)];
} else {
CGFloat const columnWidth = CGRectGetWidth([[self view] bounds]) / 2.0;
CGFloat const leftColumnX = (columnWidth - buttonSize) / 2.0;
CGFloat const rightColumnX = columnWidth + leftColumnX;
CGFloat const rowHeight = CGRectGetHeight([[self view] bounds]) / 3.0;
CGFloat const topRowY = (rowHeight - buttonSize) / 2.0;
CGFloat const middleRowY = rowHeight + topRowY;
CGFloat const bottomRowY = rowHeight * 2 + topRowY;
[[self viewOne] setFrame:CGRectMake(leftColumnX, topRowY, buttonSize, buttonSize)];
[[self viewTwo] setFrame:CGRectMake(rightColumnX, topRowY, buttonSize, buttonSize)];
[[self viewThree] setFrame:CGRectMake(leftColumnX, middleRowY, buttonSize, buttonSize)];
[[self viewFour] setFrame:CGRectMake(rightColumnX, middleRowY, buttonSize, buttonSize)];
[[self viewFive] setFrame:CGRectMake(leftColumnX, bottomRowY, buttonSize, buttonSize)];
[[self viewSix] setFrame:CGRectMake(rightColumnX, bottomRowY, buttonSize, buttonSize)];
}
}
从这里开始,您的视图将在任何设备上正确布局,因为布局取决于视图边界的宽度和高度。这是一些截图:
iPhone 4:
iPhone 5:
iPhone 6 +: