Search code examples
iosobjective-cnsnotificationcenternsnotifications

I cannot edit frame of anything after NSNotification arrived


I am using NSNotification and when notification arrive in other class then i want to change somethings in GUI. This is how i post my notification, and i m not sure if it is good way to post it or not?

   [[NSNotificationCenter defaultCenter]
     postNotificationName:@"postDetailsNotification"
     object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys: result, @"arrayDetails", nil]];

so in other class i catch it like that.

   -(void)registerNotifications
{

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receivePostDetailsNotification:)
                                                 name:@"postDetailsNotification"
                                               object:nil ];

}

 - (void) receivePostDetailsNotification:(NSNotification *) notification
  {
      NSDictionary * info =  [notification userInfo];
      inputDetails = [info objectForKey:@"arrayDetails"];
      NSLog(@"notification arriveddd=%@",[[inputDetails PostDetail] Text]);



     [self customTxtMessageViewHeight];

}

In customTXtMessageViewHelight method i only check content size of txtMessage(it is a textview) and i resize it.

 -(void)customTxtMessageViewHeight
    {

        CGFloat fl;
//MeasureHeightOfUITextView is a method to count textview height and it works without problem
        fl=[nesneResizeTextViewHeight measureHeightOfUITextView:txtMessage ];
        txtMessage.frame=CGRectMake(txtMessage.frame.origin.x, txtMessage.frame.origin.y, txtMessage.frame.size.width, fl);
        imgMessageBackground.frame=CGRectMake(imgMessageBackground.frame.origin.x, imgMessageBackground.frame.origin.y, imgMessageBackground.frame.size.width, fl);


        NSLog(@"size1=%fl",fl);
        NSLog(@"textviewsize=%fl",txtMessage.frame.size.height);

    }

Logs are correct but txtMessage's height does not change. There is no problem about iboutlets because txtMessage's height is changing if i try it in viewDidLoad method.

so then after some reading articles i get it NSNotification works in background thread and i tried to call customTxtMessageViewHeight method like that;

[self performSelectorOnMainThread:@selector(customTxtMessageViewHeight) withObject:self waitUntilDone:NO];

But nothing changed. After i tried to change how i post NSNotification

dispatch_async(dispatch_get_main_queue(),^{
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"postDetailsNotification"
     object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys: result, @"masivDetails", nil]];
});

I thought it will make it work on mainThread but it didnt work also. I really confused and will be glad to any help. Thanks.


Solution

  • Notifications are sent / received on the same thread they are posted on.

    If the logs are all fine then it looks like your frame is being re-set by something else. The usual candidate for this is Autolayout - if you're using Autolayout then you don't resize things by setting frames, you resize them by updating constraints. Otherwise, setting the frame triggers a layout pass which resets the frame back to where it was.