D
D
deleted-mezhevikin2013-10-28 16:48:32
iOS
deleted-mezhevikin, 2013-10-28 16:48:32

AirPlayButton positioning issue in MPVolumeView?

I get MPVolumeView (MediaPlayer.framework). Since there is no property with an airplay button, I search through the subview and find the button and add frame = CGRectMake(20, 20, 20, 20);
The height and width change, but the x and y positions are the same regardless of the values.
Why does this happen?
Screen:
e44de8834d4d724c6ce46560781eb785.png
Code:

//
//  ViewController.m
//

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@implementation ViewController

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor grayColor];
    [self addAirPlayButton];
    [super viewDidLoad];
}

- (void)addAirPlayButton
{
    // Зеленая вью - контейнер
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    view.backgroundColor = [UIColor greenColor];
    
    // MPVolumeView - черная
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectZero];
    volumeView.backgroundColor = [UIColor blackColor];
    // показываем кнопку airplay и скрываем слайдер громкости
    volumeView.showsRouteButton = YES;
    volumeView.showsVolumeSlider = NO;
    // Устанавливаем свою иконку на аирплей кнопку
    [volumeView setRouteButtonImage:[UIImage imageNamed:@"Airplay.png"] forState:UIControlStateNormal];

    // находим кнопку в subviews
    for (id view in volumeView.subviews)
    {
        if ([[[view class] description] isEqualToString:@"MPButton"])
        {
            // делаем кнопку оранжевой и пытаемся сделать отступ от родительского зеленого вью
            // размеры кнопки изменились а вот координаты x и y как будто не работают
            UIButton *airPlayButton = (UIButton *)view;
            airPlayButton.frame = CGRectMake(20, 20, 20, 20);
            airPlayButton.backgroundColor = [UIColor orangeColor];
        }
    }
    
    [view addSubview:volumeView];
    [self.view addSubview:view];
}

@end

github.com/nullproduction/AirPlayButton - project on github
P.S this example will only work on a real device and the airplay button will appear if there is an airplay device on the network.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Agent_Smith, 2013-10-29
@Agent_Smith

Why are you fiddling with subviews and changing the button's frame? This button is dynamically added / removed from subviews with animation, and by setting the frame with your hands, you break the entire internal layout. Make MPVolumeView sizeToFit, and move the MPVolumeView itself. If you need a larger icon, cut it to the size you want and install via Apeance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question