Rectangle 27 3

I don't know the full answer, but I do know that UIView's implementation of drawLayer:inContext: works differently depending on whether you have implemented drawRect: or not. Perhaps masking/clipping to bounds is one of those things it does differently.

You can try solving your issue a number of ways:

layer.backgroundColor = [UIColor clearColor].CGColor;
drawRect:
- (void)drawRect:(CGRect)rect {
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:30.0] addClip];
    [image drawInRect:rect];  // or whatever
}
  • carve out the corners explicitly: CGContextBeginPath(c); CGContextAddArc(c, r, r, r, M_PI, 3*M_PI_2, 0); CGContextAddLineToPoint(c, 0, 0); CGContextClosePath(c); CGContextClip(c); [[UIColor grayColor] setFill]; UIRectFill(rect);

I stole those last 2 suggestions from this great presentation from WWDC 2010: Advanced Performance Optimization on iPhone OS (video listed at this index page -- annoyingly, no direct link).

ios - UIView overriding drawRect causes view not to obey masksToBounds...

ios uiview ios6 drawrect
Rectangle 27 3

UIView

You can then load your nib as usual and use -addSubview: to add your components to the view hierarchy.

Loading the nib with

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"testView" owner:self options:nil] objectAtIndex:0];

and adding the view to the view hierarchy with

[self.view addSubview:view];

leads to the following view hiearchy:

(lldb) po [[self view] recursiveDescription]
(id) $1 = 0x088487b0 <UIView: 0x89595b0; frame = (0 0; 320 548); autoresize = W+H; layer = <CALayer: 0x8958b30>>
   | <UIView: 0x89755c0; frame = (0 0; 320 200); autoresize = RM+BM; layer = <CALayer: 0x8975080>>
   |    | <UIView: 0x8975620; frame = (200 20; 100 30); autoresize = W+H; layer = <CALayer: 0x8975680>>
   |    | <UIView: 0x8975700; frame = (20 20; 100 30); autoresize = W+H; layer = <CALayer: 0x8974ea0>>
   |    | <UIRoundedRectButton: 0x8975790; frame = (20 58; 73 44); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x8975890>>
   |    |    | <UIGroupTableViewCellBackground: 0x8976030; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x8976100>>
   |    |    | <UIImageView: 0x89768d0; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x89770f0>>
   |    |    | <UIButtonLabel: 0x8977af0; frame = (36 22; 0 0); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8977be0>>
   |    | <UIRoundedRectButton: 0x8978990; frame = (227 58; 73 44); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x8978a60>>
   |    |    | <UIGroupTableViewCellBackground: 0x8978a90; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x8978b10>>
   |    |    | <UIImageView: 0x8978b80; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8979230>>
   |    |    | <UIButtonLabel: 0x8978be0; frame = (36 22; 0 0); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8978cd0>>

Please check my edited question. You can check by yourself, loading such a XIB and adding it through [self.view addSubView:loadedNib] will only load your root view and none of its children.

Loading the NIB is OK, the problem happens when adding it to the view : [self.view addSubview:_likePreview]; adds only the root view. I need to add every subview one by one : NSArray *subviews = _likePreview.subviews; for(UIView * subview in subviews) { [self.view addSubview:subview]; }

Like I said, make sure that your UIButtons and UIViews are direct siblings of your root-view element. Set a breakpoint after [self.view addSubview:_likePreview];. Then type into the debugger po [[self view] recursiveDescription] and add the output to your question.

Returning this exact same view in a UITableViewController "viewForHeaderInSection" method works perfectly (as shown in my last code snippet from my question), showing the full component (UIView + all subviews) as a header of a table section so I am sure that my XIB is OK and that every component is a child of the root subview I am loading. I only have 1 UIView in the loaded UIView array, my root view, then containing everything as its child. Again, it does not work when invoking addSubView.

ios - UIView.addSubview using a NIB-loaded view does only add root UIV...

ios uiview addsubview loadnibnamed
Rectangle 27 0

You should use

- (void)awakeFromNib

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.

i've done it but it's actually getting called more than once. why is that ?

ios - UIView - custom view - Where does the view's subviews is initial...

ios objective-c iphone uiview
Rectangle 27 0

The view hierarchy is like a series of sheets of paper - they can be "on top" of each other, but there is no real depth. As soon as you put a 3D transform on a layer, this will have some depth and, if you've rotated it around the x axis, this will be sticking out in front of the other views.

You could try adjusting the z position of the layer to move it behind other views.

ios - Why does a transformed UIView overlap other views that are highe...

ios objective-c cocoa-touch uiview catransform3d
Rectangle 27 0

Not exactly a lot of information to go on here. The simplest solution would be to just bring View2 to the front. theSuperView here is the view that holds both View1 andView2`

theSuperView.bringSubviewToFront(View2)

Another simple solution would be to just hide View1

View1.hidden = true

There are better experiences for a user though. Transitioning to the view with an animation would be much better for this type of instance.

UIView.transitionFromView(View1, toView: View2, duration: .3, options: UIViewAnimationOptions.TransitionFlipFromLeft, completion: nil)

There is a large amount of things you can put into the options parameter here, including:

TransitionNone
TransitionFlipFromLeft
TransitionFlipFromRight
TransitionCurlUp
TransitionCurlDown
TransitionCrossDissolve
TransitionFlipFromTop
TransitionFlipFromBottom

ios - Hide a UIView behind/below other UIView and show from that view ...

ios objective-c uiview
Rectangle 27 0

Yes. You can think of views as a tree structure. So if you tell a view to be removed from the super view, the whole structure will be removed.

For example: A UIButton is actually a view which contains an UILabel which displays the title of the button. So you can do this [myUIButton removeFromSuperview];, which will remove the button and its own view hierarchy (including the button's containing UILabel).

Remark: If you only want to hide/and show a view you can also set its hidden property to YES or NO instead of removing the view from the view hierarchy. Like this it is easy to display the view again.

objective c - Does [UIView removeFromSuperview] also remove its childr...

objective-c ios uiview
Rectangle 27 0

No it doesnt. You need a member variable, as you mentioned already.

ios - Does a UIView retain its view controller - Stack Overflow

ios xcode cocoa-touch uiview uiviewcontroller
Rectangle 27 0

The effect I am wanting is for the custom UIViews to make it look like the large centered UIView middleView has holes in it

It would be a lot simpler, surely, if the UIView middleView really did have holes in it. This is easily achieved with a layer mask.

For example, this image of Venus has two square holes in it, revealing an angry-looking dude from a Stack Overflow ad behind it:

As I demonstrate, for example, here: stackoverflow.com/a/23452614/341994 The hole I made is a square, but in fact it can be any shape you like. And of course you can have more than one hole.

How would I add multiple masks. I need to have around 20 of these holes and when I use this method, only the last hole is punched in middleView.

Right - you only get one mask on a layer, but that mask can describe multiple holes.

So how do I do this? When I try and add holes the inverse happens and instead of cutting out holes it determines what isn't cut out.

ios - How to display part of a UIView at the back of a view hierarchy ...

ios objective-c xcode uiview
Rectangle 27 0

@property CGFloat zPosition;

The layers position on the z axis. Animatable. The default value of this property is 0. Changing the value of this property changes the the front-to-back ordering of layers onscreen. This can affect the visibility of layers whose frame rectangles overlap. The value of this property is measured in points.

ios - Why does a transformed UIView overlap other views that are highe...

ios objective-c cocoa-touch uiview catransform3d
Rectangle 27 0

MyView
MyView *myView = (MyView *)self.view;
myView.foo = @"Whatever";

If you want to simplify this, you can also subclass UIViewController and then just always reference self.myView in your MyViewController subclasses instead of self.view:

@interface MyViewController : UIViewController

@property (nonatomic, strong) MyView *myView;

@end


@implementation MyViewController
@synthesize myView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
            self.myView = (MyView *)self.view;
    }
    return self;
}
@end

will need such casting every single time? that's quite troublesome... also I tried defining my own view method in the ViewController that returns (MyView *) and the code does return (MyView *) super.view; but still... Xcode won't autocomplete and will still complain no such property for UIView

You need to redefine the view property in your UIViewController subclass, to be of your specific subclass, if you are going to use self.view a lot and want to avoid casting.

iphone - On iOS, why does no such property exist when ViewController's...

iphone objective-c ios uiview uiviewcontroller
Rectangle 27 0

MyView
MyView *myView = (MyView *)self.view;
myView.foo = @"Whatever";

If you want to simplify this, you can also subclass UIViewController and then just always reference self.myView in your MyViewController subclasses instead of self.view:

@interface MyViewController : UIViewController

@property (nonatomic, strong) MyView *myView;

@end


@implementation MyViewController
@synthesize myView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
            self.myView = (MyView *)self.view;
    }
    return self;
}
@end

will need such casting every single time? that's quite troublesome... also I tried defining my own view method in the ViewController that returns (MyView *) and the code does return (MyView *) super.view; but still... Xcode won't autocomplete and will still complain no such property for UIView

You need to redefine the view property in your UIViewController subclass, to be of your specific subclass, if you are going to use self.view a lot and want to avoid casting.

iphone - On iOS, why does no such property exist when ViewController's...

iphone objective-c ios uiview uiviewcontroller
Rectangle 27 0

And don't forget to set the content size !! (every noobs - including me - are always asking the same question, why my scrollView does not work?)

[scrollView setContentSize:CGSizeMake(1000, 2000)]

I made an app with custom maps, and I used an UIScrollView to make the job: it was pretty good at it.

You just have to add each scrollable element as subviews of your UIScrollView. And set the contentSize to fit to the subviews' frames, of course.

I mean objects placed at the top-left are always staying at the top-left - even after I scrolled it and it bounces back to the top-left position.

I'm going to try setting the size and see what happens.

do you want infinite scroll ? If so what do you display after your last element ? If so again, you should set the contentSize dynamically

OK. I changed the UIView into UIScrollView and set the content size, and it worked! Thanks!

uiview - How do I create a view in iOS 5 which can scroll in any direc...

ios uiview uiviewcontroller uiscrollview
Rectangle 27 0

The effect I am wanting is for the custom UIViews to make it look like the large centered UIView middleView has holes in it

It would be a lot simpler, surely, if the UIView middleView really did have holes in it. This is easily achieved with a layer mask.

For example, this image of Venus has two square holes in it, revealing an angry-looking dude from a Stack Overflow ad behind it:

As I demonstrate, for example, here: stackoverflow.com/a/23452614/341994 The hole I made is a square, but in fact it can be any shape you like. And of course you can have more than one hole.

How would I add multiple masks. I need to have around 20 of these holes and when I use this method, only the last hole is punched in middleView.

Right - you only get one mask on a layer, but that mask can describe multiple holes.

So how do I do this? When I try and add holes the inverse happens and instead of cutting out holes it determines what isn't cut out.

ios - How to display part of a UIView at the back of a view hierarchy ...

ios objective-c xcode uiview
Rectangle 27 0

Create a new UIWindow with a windowLevel of UIWindowLevelStatusBar. This will keep your activity view on top of every other view except alert views.

self.activityWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.activityWindow.windowLevel = UIWindowLevelStatusBar;
[self.activityWindow addSubview:activityView];
self.activityWindow.hidden = NO; //or call makeKeyAndVisible

would this work if I have presented a view controller as well?

Yes it would. The view controller that you are presenting would be in the window behind the window that has the activity view.

awesome it worked as expected, except that now none of my touch events work.. Maybe I have to set hidden to YES?

well the hidden to Yes worked.. now I have to take care of is the orientation.. urgh!

Prior to iOS8, there is no built in way to support orientation changes. Listening for the status bar orientation change notification is one approach that works for iOS2+.

ios - bring UIView On top of my model view controller - Stack Overflow

ios objective-c cocoa-touch ios7
Rectangle 27 0

No you can't do that... if you bringSubviewToFront A then its all subviews are showing above on B.

In that case you have to hidden subview which you dont want to see.

You can use below methods to for showing A on top.

[A.superview bringSubviewToFront:A];

Or you can put B below A with this method.

[B.superview sendSubviewToBack:B];

ios - Lift UIView above view - Stack Overflow

ios xcode uiview hierarchy subview
Rectangle 27 0

There is a method bringSubViewToFront of UIView instance. You can use that method.

UIVIew *yourSubView;
[yourMainView addSubView:yourSubView];
[yourMainView bringSubviewToFront:yourSubView];

in your case it should be like,

[[tableView cellForRowAtIndexPath:indexPath] insertSubview:subSelectionView aboveSubview:self.view];
[[tableView cellForRowAtIndexPath:indexPath] bringSubviewToFront:subSelectionView];

thanks jennis, but still does not work for me...

please see the last part.. thanks..

@AldeeMativo: bringSubviewToFront: only works with a direct subview, not any descendant view. You need to send the message to the table view cell, not the view controller's view.

iphone - Place UIView on top of all other views - Stack Overflow

iphone objective-c ios uiview uikit
Rectangle 27 0

The view hierarchy is like a series of sheets of paper - they can be "on top" of each other, but there is no real depth. As soon as you put a 3D transform on a layer, this will have some depth and, if you've rotated it around the x axis, this will be sticking out in front of the other views.

You could try adjusting the z position of the layer to move it behind other views.

ios - Why does a transformed UIView overlap other views that are highe...

ios objective-c cocoa-touch uiview catransform3d
Rectangle 27 0

No, removing a view doesn't do anything to it's child views (but of course they will no longer be drawn as they aren't in the view hierarchy any more).

Of course, if you remove a view and have no other strong references to it, it may be deallocated at some point in the future (although you shouldn't care about this or assume that there are no other references). When and if it is deallocated it will remove it's child views and release them.

objective c - Does [UIView removeFromSuperview] also remove its childr...

objective-c ios uiview
Rectangle 27 0

The best place to add this view is AppDelegate. Add view in to appDelegate window will show to top front for whole application. Also it remains on top until you hide or remove this view from appDelegate window.

Create a global object of MBProgressHUD in AppDelegate class and create show/hide methods.

@property (nonatomic, strong)  MBProgressHUD   *hud;

- (void) showProgress:(NSString *)message {

    // Create a loading view
    if (!self.hud) {
        _hud = [MBProgressHUD showHUDAddedTo:fronMostView animated:YES];
        self.hud.mode = MBProgressHUDModeText;

        // Add Progress to window.
       [self.window addSubview:self.hud];
    }

    self.hud.hidden = NO;
    self.hud.labelText = [Localization localize:message];
}


- (void) hideLoadingMessage {
    self.hud.labelText = [Localization localize:@"Finished!"];
    [self.hud hide:YES afterDelay:1];
}

Thank you, but my tweak app is running at SpringBoard, where I can't find the AppDelegate .

objective c - How to add an UIView to the front most view of the whole...

ios objective-c jailbreak mbprogresshud tweak
Rectangle 27 0

Confirmed. bringSubviewToFront: causes layoutSubview to be invoked. Since my version of layoutSubviews sets the z-orders on all the views, this was undoing the z-order I was setting in the touchesBegan:withEvent code above. Apple should mention this side effect in the bringSubviewToFront documentation.

Instead of using a UIView subclass, I created a CALayer subclass named CardLayer. I handle the touch in my KlondikeView subclass as listed below. topZPosition is an instance var that tracks the highest zPosition of all cards. Note that modifying the zPosition is usually animated -- I turn this off in the code below:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
   UITouch *touch = [touches anyObject];
   CGPoint touchPoint = [touch locationInView:self];
   CGPoint hitTestPoint = [self.layer convertPoint:touchPoint
                                           toLayer:self.layer.superlayer];
   CALayer *layer = [self.layer hitTest:hitTestPoint];

   if (layer == nil) return;

   if ([layer.name isEqual:@"card"]) {
     CardLayer *cardLayer = (CardLayer*) layer;
     Card *card = cardLayer.card;

     if ([self.solitaire isCardFaceUp:card]) {
        //...                                                                                                            
        [CATransaction begin]; // disable animation of z change                                                                  
        [CATransaction setValue:(id)kCFBooleanTrue
                         forKey:kCATransactionDisableActions];              
        cardLayer.zPosition = topZPosition++; // bring to highest z

        // ... if card fan, bring whole fan to top

        [CATransaction commit];
        //...                                                                                                            
     }
     // ...                                                                                                             
   }

}

I think using UIView's for playing cards may have been a bad design choice -- I don't think they really were intended for this. I think I'll step back and use CALayer's instead.

ios - UIView bringSubviewToFront: does *not* bring view to front - Sta...

ios uiview uitouch