如何在没有IB的情况下添加调整大小

时间:2012-09-29 18:55:33

标签: objective-c ios xcode resize iphone-5

从iPhone应用程序开发开始(过去9个月)我只使用过IB。我有一个项目已经由另一个开发人员构建,我需要针对iPhone屏幕进行优化。在IB中没问题,我知道怎么做,在这个项目中,导航栏只使用代码添加,是一个图像视图。当有人使用IB时,有人可以告诉我如何调整导航栏的大小/定位吗?我试图为iphone 5屏幕增强这个应用程序。

#define BAR_FRAME CGRectMake(0,0,320.0f,43.0f)

 @implementation ICNavbarView
 @synthesize homeButton=__homeButton;
  @synthesize prevButton=__prevButton;
 @synthesize nextButton=__nextButton;
 @synthesize delegate=__delegate;

- (id)initWithFrame:(CGRect)frame
 {
LogCmd();
self = [super initWithFrame:BAR_FRAME];
if (self) {
    self.backgroundColor = [UIColor clearColor];
    self.alpha = 0.9f;

    // Add Navigation bar background       // <<<<<< navigation bar from ui image
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BgNavBarNew"]];
    [self addSubview:bgView];




    // Add back button
    __prevButton = [UIButton buttonWithType:UIButtonTypeCustom];
    __prevButton.frame = CGRectMake(30.0f, 6.0f, 29.0f, 31.0f);
    UIImage *prevButtonPressed = [UIImage imageNamed:@"BtnPrevPressed"];
    [__prevButton setImage:[UIImage imageNamed:@"BtnPrev"] forState:UIControlStateNormal];
    [__prevButton setImage:prevButtonPressed forState:UIControlStateSelected];
    [__prevButton setImage:prevButtonPressed forState:UIControlStateHighlighted];
    [__prevButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:__prevButton];

    // Add next button
    __nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    __nextButton.frame = CGRectMake(262.0f, 6.0f, 29.0f, 31.0f);
    UIImage *nextButtonPressed = [UIImage imageNamed:@"BtnNextPressed"];
    [__nextButton setImage:[UIImage imageNamed:@"BtnNext"] forState:UIControlStateNormal];
    [__nextButton setImage:nextButtonPressed forState:UIControlStateSelected];
    [__nextButton setImage:nextButtonPressed forState:UIControlStateHighlighted];
    [__nextButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:__nextButton];

    // Add home button
    __homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    __homeButton.frame = CGRectMake(145.0f, 6.0f, 31.0f, 30.0f);
    UIImage *homeButtonPressed = [UIImage imageNamed:@"BtnHomePressed"];
    [__homeButton setImage:[UIImage imageNamed:@"BtnHome"] forState:UIControlStateNormal];
    [__homeButton setImage:homeButtonPressed forState:UIControlStateSelected];
    [__homeButton setImage:homeButtonPressed forState:UIControlStateHighlighted];
    [__homeButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:__homeButton];
}
return self;
}

 - (id)init
{
return [self initWithFrame:CGRectZero];
 }

 #pragma mark - Button handlers

- (void)buttonPressed:(id)sender
{
if (sender == __prevButton) {
    [self.delegate performSelector:@selector(navBarPrevButtonPressed)];
} else if (sender == __homeButton) {
    [self.delegate performSelector:@selector(navBarHomeButtonPressed)];
} else {
    [self.delegate performSelector:@selector(navBarNextButtonPressed)];
}
}

@end

到目前为止,我尝试过这样的UIViewAutoresizingFlexibleHeight;

 // Add Navigation bar background
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BgNavBarNew"]];
    [self addSubview:bgView];


    //resize

    bgView.autoresizingMask =  UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

这似乎没有区别,导航栏没有移动

1 个答案:

答案 0 :(得分:1)

我之前使用过这个,但是我使用了setter函数而不是使用属性。我不知道这是否会改变,但尝试这样:

    [bgView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];

如果您有多个重新调整大小的选项,那么这些内容很重要。