033在导航条中添加一个滑动条

效果如下:

033在导航条中添加一个滑动条

ViewController.h

1 #import <UIKit/UIKit.h>

2 

3 @interface ViewController : UIViewController

4 @property (strong, nonatomic) UISlider *slider;

5 @property (strong, nonatomic) UILabel *lblMessage;

6 

7 @end

ViewController.m

 1 #import "ViewController.h"

 2 

 3 @interface ViewController ()

 4 - (void)layoutUI;

 5 - (void)sliderDidChange;

 6 @end

 7 

 8 @implementation ViewController

 9 

10 - (void)viewDidLoad {

11     [super viewDidLoad];

12     

13     [self layoutUI];

14 }

15 

16 - (void)didReceiveMemoryWarning {

17     [super didReceiveMemoryWarning];

18     // Dispose of any resources that can be recreated.

19 }

20 

21 - (void)viewWillAppear:(BOOL)animated {

22     [super viewWillAppear:animated];

23     [self.navigationController setNavigationBarHidden:NO animated:NO]; //默认是NO

24     [self.navigationController setToolbarHidden:NO animated:NO]; //默认是YES

25     [self.navigationItem setHidesBackButton:YES animated:NO]; //默认是YES

26     

27     self.navigationItem.leftBarButtonItem.enabled = NO;

28     self.navigationItem.rightBarButtonItem.enabled = NO;

29     

30 }

31 

32 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

33     self.navigationItem.leftBarButtonItem.enabled = YES;

34     self.navigationItem.rightBarButtonItem.enabled = YES;

35 }

36 

37 - (void)layoutUI {

38     self.navigationItem.prompt = @"移动滑块后将改变画面颜色";

39     self.navigationItem.title = @"导航条标题";

40     

41     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];

42     

43     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];

44     

45     //创建滑动条_slider

46     _slider = [[UISlider alloc] initWithFrame:self.navigationController.navigationBar.bounds];

47     _slider.minimumValue = 0.0;

48     _slider.maximumValue = 1.0;

49     _slider.value = _slider.maximumValue / 2.0;

50     [_slider addTarget:self action:@selector(sliderDidChange) forControlEvents:UIControlEventValueChanged];

51     

52     //设置导航条的标题视图为滑动条;既是取代self.navigationItem.title的内容

53     self.navigationItem.titleView = _slider;

54     

55     //创建标签_lblMessage

56     _lblMessage = [[UILabel alloc] initWithFrame:CGRectInset(self.view.bounds, 20, 20)];

57     _lblMessage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

58     _lblMessage.backgroundColor = [UIColor blackColor];

59     [self.view addSubview:_lblMessage];

60     

61     [self sliderDidChange];

62 }

63 

64 - (void)sliderDidChange {

65     CGFloat sliderVal = _slider.value;

66     UIColor *color = [[UIColor alloc] initWithRed:sliderVal green:sliderVal blue:sliderVal alpha:1.0];

67     _lblMessage.backgroundColor = color;

68 }

69 

70 @end

AppDelegate.h

1 #import <UIKit/UIKit.h>

2 

3 @interface AppDelegate : UIResponder <UIApplicationDelegate>

4 @property (strong, nonatomic) UIWindow *window;

5 @property (strong, nonatomic) UINavigationController *navigationController;

6 

7 @end

AppDelegate.m

 1 #import "AppDelegate.h"

 2 #import "ViewController.h"

 3 

 4 @interface AppDelegate ()

 5 @end

 6 

 7 @implementation AppDelegate

 8 

 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

11     ViewController *viewController = [[ViewController alloc] init];

12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

13     _window.rootViewController = _navigationController;

14     [_window addSubview:_navigationController.view];

15     [_window makeKeyAndVisible];

16     return YES;

17 }

18 

19 - (void)applicationWillResignActive:(UIApplication *)application {

20 }

21 

22 - (void)applicationDidEnterBackground:(UIApplication *)application {

23 }

24 

25 - (void)applicationWillEnterForeground:(UIApplication *)application {

26 }

27 

28 - (void)applicationDidBecomeActive:(UIApplication *)application {

29 }

30 

31 - (void)applicationWillTerminate:(UIApplication *)application {

32 }

33 

34 @end

 

你可能感兴趣的:(滑动)