Skip to content

Instantly share code, notes, and snippets.

@cook
Created February 17, 2013 14:04
Show Gist options
  • Save cook/4971614 to your computer and use it in GitHub Desktop.
Save cook/4971614 to your computer and use it in GitHub Desktop.
这段代码会令视图的内容被拉伸,但是如果drawRect:被再次调用,比如通过setNeedsDisplay,内容又会恢复原状。避免这类问题是因为绘制系统并不会因为bounds的改变而从头绘制,这个机制的行为是通过contentMode来配置的。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
MyView* mv = [[MyView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width - 50, 150)];
mv.center = self.window.center;
[self.window addSubview:mv];
mv.opaque = NO;
mv.tag = 111;
mv.backgroundColor = [UIColor whiteColor];
//mv.contentMode = UIViewContentModeScaleToFill; // This is default value.
//mv.contentMode = UIViewContentModeTopLeft;
[mv release];
[self.window makeKeyAndVisible];
[self performSelector:@selector(resize:) withObject:nil afterDelay:0.1];
return YES;
}
- (void)resize:(id)dummy
{
// 注意这段代码没有调用setNeedsDisplay
UIView* mv = [self.window viewWithTag:111];
CGRect f = mv.bounds;
f.size.height *= 2;
mv.bounds = f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment