Setting the frame of a customView to a UIBarButtonItem

I do

mapButton *topMapButton = [[mapButton alloc] init];
    [topMapButton setSize:CGSizeMake(70, 30)];
    topMapButton.frame = CGRectMake(0, STATUS_HEIGHT +7, 70, 30);

    UIBarButtonItem *barIcon = [[UIBarButtonItem alloc] initWithCustomView: topMapButton];
    [topMapButton addTarget:self action:@selector(openMap:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = barIcon;

      

where mapButton is a subclass of UIButton, with preset appearance, nothing else

No matter which frame I set for topMapButton

, it continues to be at (0,0) and not in the normal location of rightBarButtonItem.

Note that using auto autotune to set location works for placement, but crashes when pushing another controller on the stack ...

Am I doing something wrong?

+3


source to share


2 answers


Have a look at this answer

You can install cross-insert soon. override alignmentRectInsets for your uibutton subclass and swap top and left inserts.



- (UIEdgeInsets)alignmentRectInsets {
    UIEdgeInsets insets =  UIEdgeInsetsMake(top_value, left_value, bottom_value, right_value);

    return insets;
}

      

+1


source


OK found the problem.

It seems that UIBarButtonItem doesn't really care what frame the UI has, since it will set it anyway.



however my custom button MUST be of its own type, otherwise it will mess up the frame and keep it (0,0) of the app screen.

self = [UIButton buttonWithType:UIButtonTypeCustom];

      

0


source







All Articles