[Gelöst] Die wollen mich foppen, UIButton Wahnsinn! (Achtung! Optimaler Forenbeitrag, enthält tatsächlich Code)

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • [Gelöst] Die wollen mich foppen, UIButton Wahnsinn! (Achtung! Optimaler Forenbeitrag, enthält tatsächlich Code)

    Salve,

    mal wieder einer aus der Rubrik "Stirn schlägt auf Tischplatte" :cursing: in Verbindung mit einem selbstgemalten UIButton. Ergebnis soll in etwa so aussehen wie unten, nur mit gescheitem Text.

    Auf einem View stelle ich einen UIButton dar. Auf Basis einer Subclass mit folgendem Code:

    Quellcode

    1. #import "IntroductionButton.h"
    2. @implementation IntroductionButton
    3. static int BUTTON_SHADOW_HEIGHT_PADDING = 1;
    4. static int BUTTON_SHADOW_WIDTH_PADDING = 2;
    5. - (id)initWithFrame:(CGRect)frame
    6. {
    7. self = [super initWithFrame:frame];
    8. if (self) {
    9. }
    10. return self;
    11. }
    12. // Only override drawRect: if you perform custom drawing.
    13. // An empty implementation adversely affects performance during animation.
    14. - (void)drawRect:(CGRect)rect
    15. {
    16. //// General Declarations
    17. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    18. CGContextRef context = UIGraphicsGetCurrentContext();
    19. //// Color Declarations
    20. UIColor* niceButtonGradientColor = [UIColor colorWithRed: 0.837 green: 0.837 blue: 0.837 alpha: 1];
    21. //// Gradient Declarations
    22. NSArray* niceButtonGradientColors = [NSArray arrayWithObjects:
    23. (id)[UIColor whiteColor].CGColor,
    24. (id)[UIColor colorWithRed: 0.92 green: 0.92 blue: 0.92 alpha: 1].CGColor,
    25. (id)niceButtonGradientColor.CGColor,
    26. (id)[UIColor colorWithRed: 0.92 green: 0.92 blue: 0.92 alpha: 1].CGColor,
    27. (id)[UIColor whiteColor].CGColor, nil];
    28. CGFloat niceButtonGradientLocations[] = {0, 0, 0.5, 1, 1};
    29. CGGradientRef niceButtonGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)niceButtonGradientColors, niceButtonGradientLocations);
    30. //// Shadow Declarations
    31. UIColor* outerShadow = [UIColor grayColor];
    32. CGSize outerShadowOffset = CGSizeMake(1.1, 1.1);
    33. CGFloat outerShadowBlurRadius = 0.5;
    34. UIColor* innerShadow = [UIColor whiteColor];
    35. CGSize innerShadowOffset = CGSizeMake(1.1, 1.1);
    36. CGFloat innerShadowBlurRadius = 1.7;
    37. //// Abstracted Attributes
    38. CGRect niceButtonRect = CGRectMake(0.5, 0.5, rect.size.width-BUTTON_SHADOW_WIDTH_PADDING, rect.size.height-BUTTON_SHADOW_HEIGHT_PADDING);
    39. CGFloat niceButtonStrokeWidth = 0.5;
    40. CGFloat niceButtonCornerRadius = 4;
    41. //// Nice Button Drawing
    42. UIBezierPath* niceButtonPath = [UIBezierPath bezierPathWithRoundedRect: niceButtonRect cornerRadius: niceButtonCornerRadius];
    43. CGContextSaveGState(context);
    44. CGContextSetShadowWithColor(context, outerShadowOffset, outerShadowBlurRadius, outerShadow.CGColor);
    45. CGContextBeginTransparencyLayer(context, NULL);
    46. [niceButtonPath addClip];
    47. CGContextDrawLinearGradient(context, niceButtonGradient, CGPointMake(109.5, 0.5), CGPointMake(109.5, 36.5), 0);
    48. CGContextEndTransparencyLayer(context);
    49. ////// Nice Button Inner Shadow
    50. CGRect niceButtonBorderRect = CGRectInset([niceButtonPath bounds], -innerShadowBlurRadius, -innerShadowBlurRadius);
    51. niceButtonBorderRect = CGRectOffset(niceButtonBorderRect, -innerShadowOffset.width, -innerShadowOffset.height);
    52. niceButtonBorderRect = CGRectInset(CGRectUnion(niceButtonBorderRect, [niceButtonPath bounds]), -1, -1);
    53. UIBezierPath* niceButtonNegativePath = [UIBezierPath bezierPathWithRect: niceButtonBorderRect];
    54. [niceButtonNegativePath appendPath: niceButtonPath];
    55. niceButtonNegativePath.usesEvenOddFillRule = YES;
    56. CGContextSaveGState(context);
    57. {
    58. CGFloat xOffset = innerShadowOffset.width + round(niceButtonBorderRect.size.width);
    59. CGFloat yOffset = innerShadowOffset.height;
    60. CGContextSetShadowWithColor(context,
    61. CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
    62. innerShadowBlurRadius,
    63. innerShadow.CGColor);
    64. [niceButtonPath addClip];
    65. CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(niceButtonBorderRect.size.width), 0);
    66. [niceButtonNegativePath applyTransform: transform];
    67. [[UIColor grayColor] setFill];
    68. [niceButtonNegativePath fill];
    69. }
    70. CGContextRestoreGState(context);
    71. CGContextRestoreGState(context);
    72. [[UIColor darkGrayColor] setStroke];
    73. niceButtonPath.lineWidth = niceButtonStrokeWidth;
    74. [niceButtonPath stroke];
    75. //// Cleanup
    76. CGGradientRelease(niceButtonGradient);
    77. CGColorSpaceRelease(colorSpace);
    78. }
    79. @end
    Alles anzeigen


    Im ViewController versuche ich dann noch properties...

    Quellcode

    1. - (void)viewDidLoad
    2. {
    3. [super viewDidLoad];
    4. // Do any additional setup after loading the view from its nib.
    5. UIBarButtonItem* doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(finishInitialSetup)];
    6. self.navigationItem.rightBarButtonItems = @[doneButtonItem];
    7. [self.birthdateButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    8. [self.birthdateButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    9. [self.birthdateButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
    10. [self.birthdateButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    11. [[self.birthdateButton titleLabel] setFont:[UIFont boldSystemFontOfSize:14.0f]];
    12. }
    Alles anzeigen


    und einen Titel zu setzen:

    Quellcode

    1. NSLog(@"Birthdate picked: %@",selectedBirthdate.description);
    2. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    3. [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    4. [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    5. NSString* formattedDateString = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"introduction.birthdate.button.title.prefix", nil),[dateFormatter stringFromDate:selectedBirthdate]];
    6. NSLog(@"%@",formattedDateString);
    7. [self.birthdateButton setTitle:formattedDateString forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
    Alles anzeigen


    Und was passiert? Nix! X( Outlets sind alle gesetzt, auch Subclass (Im IB). Hat das was mit der Subclass zu tun? Hab schon alles mögliche ausprobiert, nix hilft. Ich habe das Gefühl, dass Apple alles immer komplizierter macht. So ein dämlicher Button
    kann einen doch nicht einen halbe Tag beschäftigen?

    Ciao. Kay.
  • Laut Dokumentation zu -drawRect: (UIView)
    If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.

    Also wenn deine Subklasse ein UIView als Parent hat, weiß ich auch nicht wo der Fehler liegt.
    Wenn es ein UIControl oder UIButton ist, dann wird der fehlende Super-Call das Problem sein.

    In der Dokumentation steht zwar 'should' und nicht 'must', aber... Nun ja, 'should not' heißt ja auch 'probiers doch, wenn du dich traust und dir dein Leben nicht lieb ist.'
    «Applejack» "Don't you use your fancy mathematics to muddle the issue!"

    Iä-86! Iä-64! Awavauatsh fthagn!

    kmr schrieb:

    Ach, Du bist auch so ein leichtgläubiger Zeitgenosse, der alles glaubt, was irgendwelche Typen vor sich hin brabbeln. :-P
  • Ok! Problem gelöst. Diese Zeile war das Problem.

    Quellcode

    1. [self.birthdateButton setTitle:formattedDateString forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];

    Anscheinend geht das chaining nicht. UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected 8|