I have 2 UIViews both with a bit off code in them.
I can subclass one to a UIViewController like this self.view addSubView:_msgView];
_msgView being a UIView.
Now _msgView is a Plain UIView that has a 0.7 Alpha to lay over the UIViewController.
now I have another UIView called menuButtons and I want to put that on top of the _msgView.
I was thinking I could use [_msgView addSubView:_menuButton]; but that does not work and gives a warning
Incompatible pointer types sending 'MenuButtons *__strong' to parameter of type 'UIView *'
How do I put a UIView on top of another UIView?
//EDITS:
#import <UIKit/UIKit.h>
#import "SlideMessageView.h"
#import "definitions.h"
#interface MenuButtons : UIView
{
UIButton *onBack;
UIButton *onForward;
UIButton *onRefresh;
UIButton *onHome;
UIButton *onSafari;
UIButton *onChrome;
UIButton *onPocketReader;
UIButton *onSavePDF;
UIButton *onPrint;
UIButton *onShare;
SlideMessageView *msgView;
BOOL viewVisible;
}
The UIButtons have their #properties set below that.
you do 1 thing.. if u are generating both the views through code then add both views in self.view itself and set their frames separately.
i.e if 1st view is at top set the frame msgview.frame = CGRectMake(0,0,320,100);
ans for second view someview.frame = CGRectMake(0,100,320,200);
Related
I'm working on storyboard with Autolayout. I have a viewController (i.e. myController) and I dragged in it a UIView (i.e. myView).
in myController.h I declared an IBOutlet for myView:
#property (weak, nonatomic) IBOutlet UIView *myview;
and later I have associated this with my UI over storyboard (ctrl+dradding from myController's outlets).
The point is that I thought I could see myView's size from myController viewDidLoad Method:
NSLog(#"pag h:%f e w:%f",myView.frame.size.height, myview.frame.size.width);
But both of these outputs are 0. Where I could possibly be wrong?
If you are using autolayout, then at viewDidLoad layout has not happened yet. The frames at this point are still equal to CGRectZero when the views are loaded from the storyboard.
Log this again in viewDidAppear: and you will see the frame after layout is complete.
I have a UITableViewController and I need to resize the table vertically.
I can't use a UIViewController with a table inside.
There is a method?
Thanks!
This is only possible if your UITableViewController view is manually added to another superview. However, if you are planning to use the table view in a navigation controller or as a modal sheet you do not have control over the table view's size.
To have full control over the table view size:
Make you own subclass of UIViewController with UITableView outlet and a resource file.
Place a UITableView on top of its content view and resize it accordingly.
Make your view controller implement UITableViewDataSource and UITableViewDelegate protocols.
Connect outlets.
Instead of using a UITableViewController, it may be easier just to use a standard UIViewController, with a UITableView property.
E.g.:
#interface ContentsPopover : UIViewController <UITableViewDelegate, UITableViewDataSource>
//...
#property (nonatomic, strong) UITableView *theTableView;
//...
#end
That way, inside your UIViewController you can have the following code (possibly in viewDidLoad):
theTableView = [[UITableView alloc] initWithFrame:/*...*/ style:UITableViewStylePlain];
theTableView.delegate = self;
theTableView.dataSource = self;
theTableView.scrollEnabled = YES;
[self.view addSubview:theTableView];
// You can now change theTableView's frame property as needed
This is handy for when a UITableViewController wasn't quite what you're looking for.
I'm having a hard time implementing a simple scroll on my detail view.
The app is straightforward with a Master and Detail views.
When the user taps an item on Master, the Detail view is pushed with larger photo, blog text and etc.
I would like the entire Detail view to scroll, so if the picture is tall, or the text is long, they can scroll vertically to see/read more. I do not want the user to scroll these items individually. It should feel like webpage scrolling.
Currently my Detail view loads OK but I can't make it scroll.
My DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController {
IBOutlet UILabel *postTextLabel; // wired to Text Label
IBOutlet UILabel *postAuthorNameLabel; // wired to Author Label
}
#property (strong, nonatomic) id detailItem;
#end
My DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
- (void)configureView
{
if (self.detailItem) {
NSDictionary *post = self.detailItem;
NSString *postText = [post objectForKey:#"post_text"];
NSString *postAuthorName = [post objectForKey:#"post_author_name"];
postTextLabel.text = postText;
postAuthorNameLabel.text = postAuthorName;
}
}
#end
Structure on IB:
Any ideas on what's missing to make this work?
I would do the following:
1)(optional)turn your View into a scrollview by dragging it into the view in the scruture list on the side.
2)link the scrollView into your viewcontroller .h and make an Outlet connection something like this
#property (strong, nonatomic) IBOutlet UIScrollView *scroller;
(make sure you add #synthesize in the .m if you add this manually)
and make sure it is connected in IB!
3)set the contentsize of the scrollview in viewDidLoad method
scroller.contentSize = CGSizeMake(320,550);
NOTE: IBOutlet UILabel *postTextLabel; should actually probably be a UITextView so you can access ContentSize
Which would allow for the following.
CGRect frame = postTextLabel.frame;
frame.size = postTextLabel.contentSize;
postTextLabel.frame = frame;
scroller.contentSize = CGSizeMake(320, frame.size.height+200);//200 or how ever much space is above the textView
and again that only works if you use a UITextView
for ipad replace 320 with 768 or 1024, or whichever depending on orientation
The best way to connect it in IB is like this holding down control and dragging to the .h file
and make sure it is set to automatic like in the picture, and pointing to> the .h of your view.
Adding it like this also automatically adds the #synthesize for you.
Make sure UserInteractionsEnabled are checked here for our scrollview
This is how I use a XIB view controllers view as a subview in the MainViewController:
UIViewController *nameController = [[NameSubViewController alloc] initWithNibName:#"NameSubViewController" bundle:nil];
nameSubView = [nameController view];
[self.view addSubview:nameSubView];
The nameController view contains a UITextField property. Can I somehow access this property from MainViewController?
EDIT:
If I create a property for the textField in the View Controller too, I'm still not able to get it in MainViewController. Think it's because of the UIView subclassed causing problems?
NameSubViewController.h:
#import <UIKit/UIKit.h>
#interface NameSubViewController : UIViewController
#property (nonatomic, strong) IBOutlet UITextField *textField;
#end
NameSubView.h:
#import <UIKit/UIKit.h>
#interface NameSubView : UIView
#property (nonatomic, strong) IBOutlet UITextField *textField;
- (IBAction)textFieldReturn:(id)sender;
#end
Connections in IB for NameSubViewController:
Connections, NameSubViewController
Connections in IB for NameSubView:
Connections, NameSubView
Did you create an outlet for the textfield in your nameController? Do that, and then you can retrieve the value using
nameController.textField.text
In the nib file, click on the file's owner icon (on the left of the nib), and set it's class to your UIView's subclass using the inspector on the right.
Why are you adding view from Controller A in Controller B. Isn't it better that you create a view by only using its xib file and add that view to your controllers view.
For accessing field, you can assign a tag to your textfield, and where ever in your code you need it, call
self.view viewWithTag:<#(NSInteger)#> method.
I have an UIView created in IB with some labels and custom colours and linked that as an IBOutlet so I could have access to it in my viewController.
#property (strong, nonatomic) IBOutlet UIView *tile;
although I would like to create more of this outlet within a loop:
- (void)viewDidLoad {
[super viewDidLoad];
int i = 0;
while (i <= 9) {
UIView *cTile = [self.tile copy];
[self.view addSubview:cTile];
i += 1;
}
}
So I'm trying to copy this tile outlet and then add the copied view to the main view.
Apparently this can't be done and returns an error.
Is it possible to achieve such behaviour and re-use/duplicate an IBOutlet?
I dont think what you want to do is possible as UIView doesn't conform to the NSCopying protocol.
Best bet would be to create your "tile" as a UIView subclass or crate a nib for it and then you can reload it as many times as you want.