Jaki jest najprostszy sposób dodania UIToolBar do UITableViewController? Jestem zależny od funkcji edycji, więc nie mogę łatwo zmienić UITableViewController na UIViewController.Dodaj pasek narzędzi do UITableViewController
Odpowiedz
Nie ma problemu, UITableViewController
jest podklasą UIViewController
. I tak się składa, że w systemie iPhone OS 3.0 każda UIViewController
(i podklasy) może działać w połączeniu z UINavigationController
w celu zapewnienia kontekstowego paska narzędzi.
Aby to zadziałało musisz:
- Upewnij się, że używasz
UINavigationController
zawierać wszystkie Państwa zdaniem kontrolerów że potrzebuje pasek narzędzi. - Ustawia właściwość
toolbarsItems
kontrolera widoku, który chce mieć pasek narzędzi.
To prawie tak proste, jak ustawienie tytułu kontrolera widoku i powinno być wykonane w ten sam sposób. Najprawdopodobniej przez przesłonięcie inicjatora initWithNibName:bundle:
. Jako przykład:
-(id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle;
{
self = [super initWithNibName:name bundle:bundle];
if (self) {
self.title = @"My Title";
NSArray* toolbarItems = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addStuff:)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:@selector(searchStuff:)],
nil];
[toolbarItems makeObjectsPerformSelector:@selector(release)];
self.toolbarItems = toolbarItems;
self.navigationController.toolbarHidden = NO;
}
return self;
}
Można również użyć setToolbarItems:animated:
zamiast przypisywania do własności toolbarItems
, dodawać i usuwać elementy paska narzędzi w animowanym mody w locie.
Aby przepis PeyloW do pracy, musiałem dodać następującą dodatkową linię kodu:
self.navigationController.toolbarHidden = NO;
nadzieję, że pomoże ...
Uzgodnione. Musiałem umieścić to wywołanie w metodzie viewDoLoad, a nie na overRide initWithNibName. Wtedy działa świetnie. –
właśnie uratowałeś mój dzień, dziękuję –
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create a button
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
[[self tableView] reloadData];
}
- (void) info_clicked:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
[toolbar removeFromSuperview];
}
aw Swift 3:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Initialize the toolbar
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
//Set the toolbar to fit the width of the app.
toolbar.sizeToFit()
//Caclulate the height of the toolbar
let toolbarHeight = toolbar.frame.size.height
//Get the bounds of the parent view
let rootViewBounds = self.parent?.view.bounds
//Get the height of the parent view.
let rootViewHeight = rootViewBounds?.height
//Get the width of the parent view,
let rootViewWidth = rootViewBounds?.width
//Create a rectangle for the toolbar
let rectArea = CGRect(x: 0, y: rootViewHeight! - toolbarHeight, width: rootViewWidth!, height: toolbarHeight)
//Reposition and resize the receiver
toolbar.frame = rectArea
//Create a button
let infoButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(infoClicked))
toolbar.items = [infoButton]
//Add the toolbar as a subview to the navigation controller.
self.navigationController?.view.addSubview(toolbar)
}
func infoClicked() {
//Handle Click Here
}
To działa doskonale dla mnie. Nie mogłem dodać 'UINavigationController', więc ręcznie dodany pasek narzędzi był jedyną dostępną opcją. Dzięki! – codingFriend1
Nice. Myślę, że to powinna być akceptowana odpowiedź. Chciałem ** dodać pasek narzędzi do uitableviewcontroller **, nie włączając uinavigationcontroller. – soemarko
- 1. Dodaj informacje do matplotlib Pasek narzędzi nawigacyjnych/pasek stanu?
- 2. Pasek narzędzi - dodaj przycisk w górę
- 3. Dodaj niestandardowy pasek narzędzi do CKEditor w szynach
- 4. Windows 7 Pasek zadań Pasek narzędzi lub pasek narzędzi
- 5. Dodaj przycisk na UITableViewController (Swift)
- 6. Dodaj pasek przewijania do tabeli
- 7. Pasek narzędzi Android niewidoczny
- 8. Pasek narzędzi i nawigacjaDrawer
- 9. Pasek narzędzi Android pokrywający zawartość
- 10. Pasek narzędzi Android collapseMode wydanie
- 11. Dynamicznie zmieniaj pasek narzędzi CKEditor
- 12. BlackBerry - pasek narzędzi menu użytkownika
- 13. Whatsapp jak Zwijany pasek narzędzi
- 14. Pasek narzędzi UIPopoverController u góry
- 15. Pasek narzędzi - findViewbyID wraca null
- 16. Dodaj pasek graficzny do StringGrid col
- 17. Dodaj pasek przewijania do svg kontenera
- 18. Dodaj pasek kolorów do istniejącej osi
- 19. Przejrzysty pasek narzędzi dla rozwijanego paska narzędzi zwijania
- 20. Połączyć pasek narzędzi i pasek tytułu w Qt
- 21. Jak dostosować pasek narzędzi w Eclipse?
- 22. Pasek narzędzi Eclipse Juno No Debug?
- 23. Eclipse + Git - Jak uzyskać pasek narzędzi?
- 24. Pasek narzędzi AppCompat v21 zmieniający rozmiar logo
- 25. Jak dodać pasek narzędzi w PreferenceActivity
- 26. Pasek narzędzi Nakładka na zawartość treści
- 27. Pasek narzędzi Motyw ShareActionProvider jest zawsze ciemny
- 28. Kątowy materiał sidenav i ustalony pasek narzędzi
- 29. Pasek narzędzi z nieprawidłową wysokością i cieniem
- 30. Pasek narzędzi u góry UIPIckerView w Xcode?
Czy sterownik NavigationController jest wymagany? Chcę dodać ToolBar do TableViewController, który nie jest częścią NavigationController. Czy muszę używać kontrolera NavigationController, mimo że będzie on zawierał tylko jeden widok? –
@sirjorj Tak, '' UINavigationController' jest wymagany, aby uzyskać obsługę * darmowego * paska narzędzi. Bez niego musisz zarządzać własną instancją widoku "UIToolbar". – PeyloW
co, jeśli nie chcę umieszczać przycisków na tym pasku narzędzi, zamiast tego chcę umieścić tylko obraz w środku, co zrobiłbym inaczej? Dzięki. –