iOS App视图和导航设计自定义类

时间:2013-03-12 22:08:12

标签: iphone ios uinavigationbar navbar

在我的iOS应用程序中,我在下面的代码中剪切了一遍又一遍的重复。

我试图将该方法强制转换为NSObject类,但是我收到了使用“navigationItem”的错误。

-(void)customDesign {
//background pattern
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"BG-pattern.png"]];

// nav bar
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bar.png"] forBarMetrics:UIBarMetricsDefault];

//back button color  #2974c3
[self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:41.0/255.0f green:116.0/255.0f blue:195.0/255.0f alpha:1.0]];

//settings button
UIImage* settingsImage = [UIImage imageNamed:@"ButtonMenu.png"];
CGRect frameimg = CGRectMake(0, 0, settingsImage.size.width, settingsImage.size.height);
UIButton *uiSettingsButton = [[UIButton alloc] initWithFrame:frameimg];
[uiSettingsButton setBackgroundImage:settingsImage forState:UIControlStateNormal];
[uiSettingsButton addTarget:self action:@selector(menuButton) forControlEvents:UIControlEventTouchUpInside];
[uiSettingsButton setShowsTouchWhenHighlighted:YES];
//add buton to navbar
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithCustomView:uiSettingsButton];
self.navigationItem.leftBarButtonItem = settingsButton;

}

1 个答案:

答案 0 :(得分:0)

您应该创建自己的UIViewController或UITableViewController的子类(无论您使用哪个),并将此代码放在该子类的viewDidLoad方法中。然后,所有需要此代码的控制器都可以扩展您的自定义类。这将为您节省在任何地方编写此代码的麻烦。

您的自定义子类:

@interface CustomViewController : UIViewController

@end

子类中的viewDidLoad方法:

@implementation CustomViewController
    - (void)viewDidLoad {
        [super viewDidLoad];

        // all your custom code here...
    }
@end

扩展您想要完成常见工作的控制器的自定义类:

@interface VisuallyChangedViewController : CustomViewController

@end

@implementation VisuallyChangedViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
@end