¿Es posible en iOS que una vista siempre flote sobre todas las demás vistas? Pregunto esto porque lo que me gustaría lograr es una vista que flota sobre un ViewController, y luego se desliza un Controlador de Vista Modal, mientras esa vista particular todavía está flotando sobre ese Controlador de Vista Modal (espero que entiendas lo que estoy tratando de decir)Vista flotante sobre todos los ViewControllers
5
A
Respuesta
8
Hay. Puede agregar su vista al window
principal y llevarlo al frente cuando sea necesario.
En el siguiente código se supone que _viewConroller
y _anotherView
son propiedades sólidas de appDelegate: la configuración podría ser diferente.
Este código agregaría un pequeño cuadrado azul en la esquina superior izquierda de la pantalla.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
_anotherView = [[UIView alloc] initWithFrame: CGRectMake (0.0,0.0,20.0,20.0)];
[anotherView setBackgroundColor: [UIColor blueColor]];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[self.window addSubView: _anotherView];
[self.window bringSubViewToFront: _anotherView]; //not really needed here but it doesn't do any harm
return YES;
}
3
Usted puede hacer lo siguiente si está utilizando guión gráfico y el diseño automático (inspirado en la primera respuesta)
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *_vc = [sb instantiateViewControllerWithIdentifier:@"FloatingController"];
_anotherView = _vc.view;
[_anotherView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.window addSubview: _anotherView];
[[VLBConstraintsGenerator sharedInstance] setWidth:200 forView:_anotherView inSuperView:_window];
[[VLBConstraintsGenerator sharedInstance] setHeight:200 forView:_anotherView inSuperView:_window];
[[VLBConstraintsGenerator sharedInstance] setLeading:0 forView:_anotherView inSuperView:_window];
[_anotherView setBackgroundColor:[UIColor grayColor]];
[[_anotherView layer] setBorderWidth:1];
[[_anotherView layer] setBorderColor:[UIColor yellowColor].CGColor];
[self.window makeKeyAndVisible];
[self.window bringSubviewToFront:_anotherView]; //not really needed here but it doesn't do any harm
todo lo que necesita hacer es arrastrar un controlador de vista en que guión gráfico principal con FloatingController como un ID de guión gráfico
Métodos adicionales
-(void)setWidth:(CGFloat)theWidth forView:(UIView *)theView inSuperView:(UIView *)theSuperView
{
assert([theSuperView isEqual:theView.superview]);
NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:theView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:theWidth];
// [cn setPriority:999];//make it variable according to the orientation
[theSuperView addConstraint:cn];
}
-(void)setHeight:(CGFloat)theHeight forView:(UIView *)theView inSuperView:(UIView *)theSuperView
{
assert([theSuperView isEqual:theView.superview]);
NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:theView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:theHeight];
[theSuperView addConstraint:cn];
}
-(void)setLeading:(CGFloat)theLeading forView:(UIView *)theView inSuperView:(UIView *)theSuperView
{
assert([theSuperView isEqual:theView.superview]);
NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:theView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:theSuperView
attribute:NSLayoutAttributeLeading
multiplier:1
constant:theLeading];
[theSuperView addConstraint:cn];
}
Cuestiones relacionadas
- 1. AdMod instancia única en todos los ViewControllers
- 2. Android vista flotante (sobre otras vistas)
- 3. Vista flotante sobre UITableView en RootViewController
- 4. iPhone 3.1 SDK: la categoría UIViewController está afectando TODOS ViewControllers
- 5. Iterar sobre todos los valores dobles posibles
- 6. Posicionamiento CSS Sobre todos los elementos
- 7. la manera de recorrer todos los UIViewControllers sobre la aplicación
- 8. Flotante de control sobre otros controles en WPF
- 9. Segue entre ViewControllers sin control de desencadenamiento?
- 10. Android botones flotantes sobre una vista
- 11. Conmutar ViewControllers con UISegmentedControl en iOS5
- 12. iterando sobre todos los directorios en un archivo zip java
- 13. Solr - consultas sobre todos los campos de las mejores prácticas
- 14. ¿Cómo obtengo todos los artículos sobre personas de Wikipedia?
- 15. iOS: reenviar todos los toques a través de una vista
- 16. Cómo guardar una configuración para Todos los usuarios en Vista
- 17. Cómo eliminar la lista vista de todos los elementos
- 18. Obtener todos los elementos iframe
- 19. Comparando los valores de punto flotante
- 20. viewWillAppear en viewcontrollers de una barra de pestañas
- 21. ios: Segue personalizado entre dos viewcontrollers en modo apaisado
- 22. Si todos los bits son 0, ¿cuál es el valor del punto flotante IEEE?
- 23. Iterar a través de todos los posibles valores de coma flotante
- 24. ¿Cómo agregar vista superpuesta sobre otra vista en android?
- 25. ¿Es correcto tener ViewControllers con un montón de código?
- 26. "refrescar" punto de vista sobre la rotación del dispositivo
- 27. ¿Flotante o decimal para los precios?
- 28. Comprensión de los problemas de punto flotante
- 29. Pregunta sobre el puerto de vista
- 30. AdMob en todos mis controladores de vista?
Ok, ¿esto también funcionará cuando esté usando el storyboard? – thvanarkel
Creo que debería, pero no lo probé. Es posible que tengas que crear un método para traer _otras ViewToFront y llamarla después de que ViewControllers cambie de lugar. –