0

自从开始 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;

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

4

1 回答 1

1

我以前用过这个,但是我使用了 setter 函数而不是使用属性。我不知道这是否会改变任何东西,但试试这种方式:

    [bgView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];

如果您有多个调整大小的选项,则括号很重要。

于 2012-09-29T19:01:51.030 回答