Answers
这也是困扰我的问题之一。目前我想出了 不用 AutoLayout时的解决办法:如果用xib的话,请见 http://blog.csdn.net/rhljiayou/article/details/12029927 ;如果不是用xib的话,只能手动偏移了。
#define IOS_7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
#define IOS6_7_DELTA(V,X,Y,W,H) if (IOS_7) {CGRect f = V.frame;f.origin.x += X;f.origin.y += Y;f.size.width += W;f.size.height += H;V.frame=f;}
...
IOS6_7_DELTA(view, 0, 20, 60, 0);
另外,如果使用了AutoLayout,并且用了StoryBoard,那么可以很方便地将搜索框跟TopLayoutGuide做一个constraint。这个TopLayoutGuide看起来是很有用的,可以看到官方文档里面说的用法。
- If a status bar is visible onscreen and there is no visible navigation bar, the bottom of the status bar
- If a navigation bar is visible onscreen, the bottom of the navigation bar
- If there is no status bar or navigation bar visible onscreen, the top of the screen in the device’s current orientation
但是如果只是用xib的话,似乎没法在IB里面加这个constraint(这个问题见我在stackoverflow上问的 问题 ,但是目前没有解答【大概我的英文太差了?】)。如果是纯代码的话,我对constraints语法不是很熟悉,写的估计是错的,仅供参考:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *verticalConstraint = @"V:|[v]";// for iOS6
NSMutableDictionary *views = [NSMutableDictionary new];
NSMutableArray *constraints = [NSMutableArray new];
[views setObject:self.subview forKey:@"v"];
if ([self respondsToSelector:@selector(topLayoutGuide)]) {
// for iOS7
[views setObject:self.topLayoutGuide forKey:@"topLayoutGuide"];
verticalConstraint = @"V:[topLayoutGuide][v]";
}
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:verticalConstraint options:0 metrics:nil views:views]];
// ... other constraints
[self.view addConstraints:constraints];
// Do any additional setup after loading the view from its nib.
}
狂暴黑岩舒
answered 10 years, 7 months ago