iPhone控件之UIToolbar

 1 //
2 // UITestViewController.m
3 // UITest
4 //
5
6 #import "UITestViewController.h"
7
8 @implementation UITestViewController
9
10
11 - (void)buttonClick:(id)sender {
12
13 NSLog(@"you clicked button: %@",[sender title]);
14 }
15
16 - (void)viewDidLoad {
17
18 [super viewDidLoad];
19
20 CGSize viewSize = self.view.frame.size;
21 float toolbarHeight = 44.0;
22 CGRect toolbarFrame = CGRectMake(0,viewSize.height-toolbarHeight,viewSize.width,toolbarHeight);
23
24 UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
25
26 myToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth
27 |
28 UIViewAutoresizingFlexibleLeftMargin
29 |
30 UIViewAutoresizingFlexibleRightMargin
31 |
32 UIViewAutoresizingFlexibleTopMargin;
33
34 UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"button 1"
35 style:UIBarButtonItemStylePlain target:self
36 action:@selector(buttonClick:)];
37
38 UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2"
39 style:UIBarButtonItemStyleBordered
40 target:self
41 action:@selector(buttonClick:)];
42
43 UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"apple_icon.png"]
44 style:UIBarButtonItemStyleBordered
45 target:self
46 action:@selector(buttonClick:)];
47
48 UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
49 target:nil
50 action:nil];
51
52 UIBarButtonItem *trashButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
53 target:self
54 action:@selector(buttonClick:)];
55
56 NSArray *buttons = [[NSArray alloc] initWithObjects:button1,button2,button3, flexButton,trashButton,nil];
57
58 //cleanup
59 [button1 release];
60 [button2 release];
61 [button3 release];
62 [flexButton release];
63 [trashButton release];
64
65 [myToolbar setItems:buttons animated:NO];
66
67 [buttons release];
68
69 [self.view addSubview:myToolbar];
70
71 [myToolbar release];
72 }
73
74 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
75
76 return YES;
77 }
78
79 - (void)didReceiveMemoryWarning {
80 // Releases the view if it doesn't have a superview.
81 [super didReceiveMemoryWarning];
82
83 // Release any cached data, images, etc that aren't in use.
84 }
85
86 - (void)viewDidUnload {
87 // Release any retained subviews of the main view.
88 // e.g. self.myOutlet = nil;
89 }
90
91
92 - (void)dealloc {
93 [super dealloc];
94 }
95
96 @end
原文地址:https://www.cnblogs.com/foxmin/p/2393649.html