Check programmatically created UIView visible or not - ios

vwInfo = [[UIView alloc]initWithFrame:CGRectMake(20, 85, 280, 100)];
[self.view addSubview:vwInfo];
I create a UIView programmatically on button tap.when i click it again i need to check vwInfo is visible or not.I can done this with a Boolean value.is there any other option to do this?

you can check if view is exist or not using isDescendantOfView but make sure you have to pass your vwInfo's superview to check view already exist or not.
if ([vwInfo isDescendantOfView:self.view]) {
//view already exist in self.view
}
else{
//view is not exist in self.view.
}

Is it because you do not want to add the UIView a second time when you already added it ?
In this case you could declare in your .h(header) file UIView *vwInfo;
and then in your IBAction for your UIButton:
if(vwInfo == nil) {
vwInfo = [[UIView alloc]initWithFrame:CGRectMake(20, 85, 280, 100)];
[self.view addSubview:vwInfo];
}

you can check with Tag Functionality. Assign tag to your view.
UIView * vwInfo = [[UIView alloc]initWithFrame:CGRectMake(20, 85, 280, 100)];
[vwInfo setTag:101];
[self.view addSubview:vwInfo];
On Button Click
- (void)buttonClick :(id)sender {
UIView * viewTemp = (UIView *)[self.view viewWithTag:101];
if(viewTemp){
NSLog(#"View is available");
if([viewTemp isHidden]){
NSLog(#"Your view is hidden");
}else{
NSLog(#"Your view is visible");
}
}else{
NSLog(#"View is not added yet");
}
}

UIView is accessible with the superview property
if([vwInfo superview]!=nil)
NSLog(#"visible");
else
NSLog(#"not visible");

Related

How to add a custom view to JSQMessagesViewController

How can i add a custom view on the top of collection view?
I want to design a custom view using Xcode user interface
I have added my view in storyBoard view controller but the collection view covers it
so I add a custom view in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[self addViewOnTop];
}
}
-(void)addViewOnTop {
UIView *selectableView = [[UIView alloc]initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, 40)];
selectableView.backgroundColor = [UIColor redColor];
UILabel *randomViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 100, 16)];
randomViewLabel.text = #"RandomView";
[selectableView addSubview:randomViewLabel];
[self.view addSubview:selectableView];
}
this code runs successfully but how can I load a view using Xcode interface builder or load a .XIB file
now I do this
#implementation chatViewController
- (void)viewDidLoad {
[super viewDidLoad];
[SVProgressHUD dismiss];
self.collectionView.collectionViewLayout.sectionInset = UIEdgeInsetsMake(70, 0, 0, 0);
self.collectionView.dataSource = self;
self.collectionView.delegate= self;
messages = [NSMutableArray array];
[self addMessages];
self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeZero;
self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero;
self.topContentAdditionalInset = 70.0f;
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleCollectionTapRecognizer:)];
[self.collectionView addGestureRecognizer:tapRecognizer];
self.collectionView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
[self addViewOnTop];
}
-(void)addViewOnTop {
commentsheader *test = [[commentsheader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 70)];
[self.view addSubview:test];
}
but it didn't help
If I am understanding the real problem that you are having is that your customView is covering the collectionView instead of modifying the storyboard just adjust the inset of the collection view.
Add an UIEdgeInset to let you view your messages under the new view Call this in the viewDidLoad()
self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0)
You should try out currentView.bringSubview(toFront: <UIView instance>)
The problem was that the navigation bar hides the view.
But bringing it to the front didn't fix the problem
but doing this fixed it:
in viewDidLoad
self.edgesForExtendedLayout = UIRectEdgeNone;

No response of button in the headerView of UITableView

The design is that there is a return button in the headerView, but I can not get any response of the return button.
Here is some description of my code: I add a delegate in my headerView, so when I click the button in headerView, the UIViewController can return to the front one. I define a class to be headerView, and in this class I add the return button like this :
_returnButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 20, 16, 20)];
_returnButton.userInteractionEnabled = YES;
_returnButton.clipsToBounds = YES;
[_returnButton setBackgroundImage:[UIImage imageNamed:#"return.png"] forState:UIControlStateNormal];
[self addSubView:_returnButton];
Then in the UIViewController, I create an instance of my headerView and add it to my UITableView:
ThemeHeader *headerView = [[ThemeHeader alloc] init];
headerView.delegate = self;
headerView.userInteractionEnabled = YES;
[headerView updateIfNeeded];
In the function updateIfNeeded, I add the target to my headerView, just like this:
- (void)updateIfNeeded {
NSLog(#"test delegate");
if ([self.delegate respondsToSelector:#selector(return:)]) {
NSLog(#"succeed");
[_returnButton addTarget:self.delegate action:#selector(return:) forControlEvents:UIControlEventTouchUpInside];
}
}
I can see the log "test delegate" and "succeed", but when I click the return button, it seems nothing happens, and I can not see any log. What's wrong with my code?
Try to modify
[self.contentView addSubView:_returnButton];

Issue in deleting the textfield

I have two buttons for adding and deleting the textfields. Adding works fine but when I click on delete it deletes only the last added textfield. Here are my two methods:
-(void)addTextField {
keyTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, yAxisDistance, 150, 30)];
keyTextField.borderStyle = UITextBorderStyleRoundedRect;
keyTextField.placeholder = #"Key Value";
keyTextField.delegate = self;
[self.view addSubview:keyTextField];
valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(165, yAxisDistance, 150, 30)];
valueTextField.borderStyle = UITextBorderStyleRoundedRect;
valueTextField.placeholder = #"Value";
valueTextField.delegate = self;
[self.view addSubview:valueTextField];
yAxisDistance = yAxisDistance+35;
}
-(void)deleteTextField {
[keyTextField removeFromSuperview];
[valueTextField removeFromSuperview];
yAxisDistance = yAxisDistance-35;
}
I know it's an small issue but I am very new to this field so kindly help.
Use this code for remove specific textfield from UIView. But First you have to set tag of every UITextField in view when you create or add it in view.
for ( UITextField* textField in view.subviews )
{
if(textField.tag== 1)
{
[textField removeFromSuperview];
}
}
I think the problem lies with the outlet. Can you check the following:
Open the outlets that are connected to the textifeld in the IB. There should be gray dots inside circles on the left side of the editor. Are they seem correct for both text fields?
Setup a breakpoint inside deleteTextField method and check the two textfields. Verify that both properties are not nil.
PS: You don't need to add tags to your view, using properties is perfectly fine and even better in my opinion. The reason for your problem is something else. Also, you do not need to removeFromSuperview, you can also setHidden:YES.
Sorry that time I was not understand your problem...
I done with this:
Declare one
NSMutableArray *allTextfieldArray;
and initialise in
viewdidload
method..
now do:
-(void)addTextField {
keyTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, yAxisDistance, 150, 30)];
keyTextField.borderStyle = UITextBorderStyleRoundedRect;
keyTextField.placeholder = #"Key Value";
keyTextField.delegate = self;
[self.view addSubview:keyTextField];
valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(165, yAxisDistance, 150, 30)];
valueTextField.borderStyle = UITextBorderStyleRoundedRect;
valueTextField.placeholder = #"Value";
valueTextField.delegate = self;
[self.view addSubview:valueTextField];
yAxisDistance = yAxisDistance+35;
[allTextfieldArray addObject:keyTextField];
[allTextfieldArray addObject:valueTextField];
}
if ([allTextfieldArray count]>0) {
UITextField *txtField = [allTextfieldArray lastObject];
[allTextfieldArray removeLastObject];
[txtField removeFromSuperview];
txtField = nil;
UITextField *txtField2 = [allTextfieldArray lastObject];
[allTextfieldArray removeLastObject];
[txtField2 removeFromSuperview];
txtField2 = nil;
yAxisDistance = yAxisDistance-35;
}
you store the last textField in your variables keyTextField and valueTextField. So when you call your deleteTextField the last both will be deleted. you must track which textFileds you want exactly to delete.
for example you could give all your textFields a tag number when you create them:
first create an int as counter:
#implementation YourClass {
int tagcounter;
}
in init method set your counter:
tagcounter = 0;
in your addTextField:
keyTextField.tag = tagcounter++;
valueTextField.tag = tagcounter++;
when the delete button is tapped, you must know the textfield tags and pass them to your deleteTextField method. There you could do something like:
-(void)deleteTextFieldWithTag:(int)tagnumber {
for (UIView *view in [self.view subviews])
{
if ([view isKindOfClass:[UITextField class]])
{
if (view.tag == tagnumber || view.tag == tagnumber+1) {
[view removeFromSuperview];
}
}
}

Subview won't hide correctly in custom UITableViewCell on toggling of UITableView edit

So my app has an editable and sortable UITableView in its left hamburger basement:
To make sure the table cells were skinny enough to show both the delete button and the sorting drag handle on edit, I created a custom UITableViewCell to handle the editing of the table cells:
Everything works fine on edit, but when I tap done, instead of hiding the delete button hangs around:
The code for this, inside of BookmarkTableViewCell.m, is:
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if (editing) {
for (UIView * view in self.subviews) {
if([[[view class] description] isEqualToString:#"UITableViewCellReorderControl"])
{
UIView *movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
[movedReorderControl addSubview:view];
[self addSubview:movedReorderControl];
CGRect newFrame = movedReorderControl.frame;
newFrame.origin.x = -35;
movedReorderControl.frame = newFrame;
}
}
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 24, 24)];
[deleteBtn setImage:[UIImage imageNamed:#"icon_delete.png"]];
[self addSubview:deleteBtn];
}
}
Let me know if you need anymore details. Any insight into fixing this would be great. Thanks!
It looks like you're adding the deleteBtn, but you aren't removing it. if editing is false you should locate the deleteBtn cell and remove it from it's superview so it goes away.

how to check if if a UIView is still there?

i have to set some custom size parameters to a UIView according to the device orientation. i load the UIView which has NOT an own viewcontroller as follows:
in main.h
#property (nonatomic, retain) IBOutlet UIView *socialActionView;
in main.m
#synthesize socialActionView;
.
.
.
socialActionView = [[[NSBundle mainBundle] loadNibNamed:#"SocialActionViewController" owner:self options:nil] objectAtIndex:0];
[self.view addSubview:socialActionView];
if not needed anymore i remove it from the superview with
[self.socialActionView removeFromSuperview];
NSLog(#"%#",self.socialActionView.superview);
the log says (null) after removal
the size adjustments i do like this
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if ((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
if (self.socialActionView != NULL) {
[self.socialActionView setBounds:CGRectMake(0, 0, 320, 480)];
[self.socialActionView setCenter:CGPointMake(160, 240)];
[self.scroll1 setContentSize:CGSizeMake(320, 600)];
[self.scroll1 setBounds:CGRectMake(0, 0, 320, 428)];
[self.scroll1 setCenter:CGPointMake(160, 266)];
}
} else if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
if (self.socialActionView != NULL) {
[self.socialActionView setBounds:CGRectMake(0, 0, 480, 320)];
[self.socialActionView setCenter:CGPointMake(240, 160)];
[self.scroll1 setContentSize:CGSizeMake(480, 600)];
[self.scroll1 setBounds:CGRectMake(0, 0, 480, 268)];
[self.scroll1 setCenter:CGPointMake(240, 186)];
}
}
}
works ok so far until i don´t remove the subview (socialAcitonView) from my main view. cause after that the self.socialActionView is not accessable anymore. i´ve seen a lot of examples here on stackoverflow like
-(IBAction)showPopup:(id)sender {
if(![[self myView] isDescendantOfView:[self view]]) {
[self.view addSubview:[self myView]];
} else {
[[self myView] removeFromSuperview];
}
or
-(IBAction)showPopup:(id)sender
{
if (!myView.superview)
[self.view addSubview:myView];
else
[myView removeFromSuperview];
}
or
if (!([rootView subviews] containsObject:[self popoverView])) {
[rootView addSubview:[self popoverView]];
} else {
[[self popoverView] removeFromSuperview];
}
or
-(IBAction)showPopup:(id)sender {
if([[self myView] superview] == self.view) {
[[self myView] removeFromSuperview];
} else {
[self.view addSubview:[self myView]];
}
}
but none of them works. the debugger always throws an exception cause of bad access. what (from my point of view) means the object does not exist any more. but how can the logger access it after i´ve removed it from the superview and say (null)?
i know it would have been better to give the view an dedicated view controller and put didRotateFromInterfaceOrientation there so i don´t have to check if the socialActionView is there. anyhow i´d like to ask if there is a way how i can check of the UIView is (null) nor not. before i change all my code to a view/viewcontroller couple.
any hints appreciated!
You are making it a retained property and then bypassing the retain attribute when you do this:
socialActionView = [[[NSBundle mainBundle] loadNibNamed:#"SocialActionViewController" owner:self options:nil] objectAtIndex:0];
If you make that self.socialActionView = ... your property should hold its reference even after the view is removed from the subviews array.
(In fact, I'd recommend changing your synthesize statement to #synthesize socialActionView = _socialActionView; and putting a self.socialActionView wherever the compiler complains about that symbol.)

Resources