I add a view as subview of my uiviewcontroller like this:
// into my ViewController:
UIImageView *imView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"img.jpg"]];
imView.frame = CGRectMake(2, 46, 1020, 720);
[self.view addSubview:imView];
now, with another button I wish remove the imView from the subview chain..
how can I do to do this??
thanks
You can call this method, which belongs to UIView (and UIImageView inherits from UIView)
UIView - (void)removeFromSuperview
Related
I have a TableViewController which should have a "floating" header at the top of the table view which should not scroll with the content.
Is there a way to achieve this using a UITableViewController (I know this could be done with nested a Tableview inside a plain UIViewController but I would prefer to have a UITableViewController as the main ViewController so I can use the benefits of Storyboard TableView-Goodies.
Make a nib with a UITableViewController or a subclass of it and simply rearrange the table view to make room for the header view. A table view controller with a slightly different layout and more views is still a table view controller.
It might be considered a hack, but this seems to do the job! (tested with the default Master-Detail project template)
First change the type of the MasterViewController to a UIViewController and add the protocol references for the delegate and datasource. Also add an IBOutlet for a UITableView.
#interface MasterViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
Next, wire up that IBOutlet to the UITableView in the Storyboard editor.
Now add the following code to viewDidLoad:
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
header.text = #"HEADER";
header.backgroundColor = [UIColor greenColor];
[mainView addSubview:header];
self.tableView.frame = CGRectMake(0, 40, 320, 440);
[mainView addSubview:self.tableView];
self.view = mainView;
I need to add a Button or a label to a UIView and add it to my UIViewController. I did the following but it only crashed the program.
UIView *myview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 0, 300)] ;
[myview addSubview:myButton];
[self.view addSubview:myView];
The code crashes. Why is that?
You set your myview's width to 0.
You sure you created "myButton"?
You create myview but you add my*V*iew as subview. Is there any warning?
The first step you need to take is:
Make Sure you have allocated & initialized UIButton object with "initWithFrame" method.
provide a width to your UIView in "initWithframe" method, So that it can be seen.
Now use your [self.view addSubview:myView];
Please let me know if it is useful.
Use:
//Assuming that myview should have the size of the main view:
UIView *myview = [[UIView alloc] initWithFrame:self.view.bounds];
[myview addSubview:myButton]; //Whatever ...
[self.view addSubview:myview]; //There was a typo in this line
If this code block is in the init or loadView of viewcontroller, this will crash.
Make sure you have created a view for view controller in this case before adding views to self.view
UIView *aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = aView;
[aView release];
myButton = [[UIButton alloc] init....
.....
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 0, 300)] ;
[myView addSubview:myButton];
[self.view addSubview:myView];
Try updating the view with the following:
[myview setNeedsDisplay];
and remove the second call to addSubView (which is probably causing the crash):
[self.view addSubview:myView];
I am trying to put image in background of UIView.
Problem is that my view(subView) is inside the main view.
It's simple as:
yourSubView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"BackgroundImage.png"]];
Class:
#interface abc : UIView {
.......
}
In class define subview:
IBOutlet UIView* subView;
set background image of subView as:
UIColor* bgrColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed: backgroundImageName]];
subView.backgroundColor = bgrColor;
Finally add subview to the View
[self.view addSubView:subView];
In my Project, I have a customised #interface GraphView: UIView. Hence GraphView is a subclass of UIView and is meant to show a graph.
Then, I create a new View Controller called Summary using a NIB. In the Interface builder, I just add a UIToolbar at the bottom of the View.
In the implementation of Summary, in the viewDidLoad method, I have the following code:
-(void)viewDidLoad {
[super viewDidLoad];
GraphView *myGraph = [[GraphView alloc]init];
[self.view addSubview:myGraph]; //this does not work
}
But I am unable to get this.
I believe you need to set the frame of myGraph before you add it as a subview to self.
CGRect rect = CGRectInset(self.view.bounds, 0.0, 0.0);
GraphView *myGraph = [[GraphView alloc] initWithFrame:rect];
[self.view addSubview:myGraph];
i want to add an UIView for small size as a subview to existing view.
i have a UIViewController pushed with a nib name which is the main view, i am trying to create an object of UIView and alloc it with dimension with the following code in the viewcontroller's viewDidLoad methoad
UIView *view = [[UIView alloc] init];
but the idea is not suggesting the alloc on UIView and throwing an error if i forcefully type and run
please help me out with this
To add something (rather important) to the answers above;
CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view = [[UIView alloc] initWithFrame:frame];
Then, you want to actually add it to the superview (assuming the view is self.view)
[self.view addSubview:view];
UIView needs to be alloc'ed and init'ed with a frame:
CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view = [[UIView alloc] initWithFrame:frame];
UIView *view = [[UIView alloc] init];