Permalink
Browse files

working code

  • Loading branch information...
mlaster committed Dec 22, 2011
1 parent 67497e2 commit f1cfa799cdcaffdd8bd652116bff939f7466bef6
Showing with 17 additions and 6 deletions.
  1. +16 −5 CenterTest/TestViewController.m
  2. +1 −1 CenterTest/en.lproj/MainStoryboard_iPad.storyboard
@@ -20,7 +20,7 @@ - (void)didReceiveMemoryWarning
- (void)viewDidLoad
{
[super viewDidLoad];
- [self.button setTitle:@"Dynamic title contents" forState:UIControlStateNormal];
+ [self.button setTitle:@"This can be any width Dynamic title contents" forState:UIControlStateNormal];
[self.button sizeToFit];
// Do any additional setup after loading the view, typically from a nib.
}
@@ -34,16 +34,27 @@ - (void)viewDidUnload
- (void)viewWillAppear:(BOOL)animated
{
- NSLog(@"superview: %@", self.button.superview);
- NSLog(@"superview.width: %f", self.button.superview.frame.size.width);
- NSLog(@"superview.height: %f", self.button.superview.frame.size.height);
- self.button.center = self.button.superview.center;
+// self.button.center = self.button.superview.center;
+// self.button.center = CGPointMake(self.button.superview.bounds.size.width/2, self.button.superview.bounds.size.height/2);
[super viewWillAppear:animated];
}
+- (void)viewDidLayoutSubviews {
+ [super viewDidLayoutSubviews];
+ self.button.center = [self.button convertPoint:self.button.superview.center fromView:self.button.superview.superview];
+}
- (void)viewDidAppear:(BOOL)animated
{
+ CGRect workframe = CGRectZero;
+
[super viewDidAppear:animated];
+ self.button.center = CGPointMake(self.button.superview.bounds.size.width/2, self.button.superview.bounds.size.height/2);
+
+ // Align on a pixel boundary to avoid fuzzy text
+ workframe = self.button.frame;
+ workframe.origin.x = floorf(self.button.frame.origin.x);
+ workframe.origin.y = floorf(self.button.frame.origin.y);
+ self.button.frame = workframe;
}
- (void)viewWillDisappear:(BOOL)animated
@@ -15,7 +15,7 @@
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="Yx7-CP-eth">
<rect key="frame" x="172" y="186" width="159" height="37"/>
- <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
+ <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" title="Center This Button">
<color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>

0 comments on commit f1cfa79

Please sign in to comment.