Rectangle 27 41

@interface UIView (RemoveConstraints)

- (void)removeAllConstraints;

@end


@implementation UIView (RemoveConstraints)

- (void)removeAllConstraints
{
    UIView *superview = self.superview;
    while (superview != nil) {
        for (NSLayoutConstraint *c in superview.constraints) {
            if (c.firstItem == self || c.secondItem == self) {
                [superview removeConstraint:c];
            }
        }
        superview = superview.superview;
    }

    [self removeConstraints:self.constraints];
    self.translatesAutoresizingMaskIntoConstraints = YES;
}

@end

After it's done executing your view remains where it was because it creates autoresizing constraints. When I don't do this the view usually disappears. Additionally, it doesn't just remove constraints from superview but traversing all the way up as there may be constraints affecting it in ancestor views.

This should be the official answer.

why do you have self.translatesAutoresizingMaskIntoConstraints = YES; ? you literally never want this for views you are setting up with constraints?

I'm removing the constraints, so I want to use the autoresize constraints after I remove the other constraints to hold it in place, without that line in my code the view would disappear

ios - Remove all constraints affecting a UIView - Stack Overflow

ios objective-c uiview constraints autolayout
Rectangle 27 1

You are not using the alert API correctly. You're leaving out the essential piece that gets it on the screen -- the View Controller itself. You need:

without the Alert, since it seems you don't want that:

func removeTags(sender: UIButton) {
    guard sender.tag < self.locationNameStrs.count else {
        print("Invalid tag index \(sender.tag)!")
        return
    }
    self.locationNameStrs.removeAtIndex(sender.tag)
    self.outletView.removeFromSuperview()
    self.dislayOutletTags()
}

You should not be iterating through the scroll view to remove every view you find. This is fragile & awkward design. You're already keeping track of the view added, so remove it by name.

Your isKindOfClass() in your solution makes no sense, because by definition a view's subviews are all subclasses of UIView.

Again, your if statement makes no sense, since you're looking at an item in the array, and asking if that very array contains it.

Your one does not mention which UIView are gonna be removed.

It explicitly removes outletView as you see right in the code. Everything that has been added as a subview to outletView is automatically removed as a result. Try it and see.

Then you need to track which one was tapped, store it in a class property (self.outletView or otherwise), so it can be removed from your removeTags method. You haven't given enough context, so you're responsible for fixing up that part yourself. THe answer was just to help you make a proper UIAlertController and also to remove the redundancies regarding isKindOfClass and .contains. The rest you're responsible for, unless you want to post the entire class code in another question. Probably best for the code review forum to get an overall critique.

ios - How to remove UIView or UIButton from superview inside UIScrollV...

ios swift uiview uiscrollview uibutton
Rectangle 27 200

If my superview is a box measuring 10 units on each side, and my subview is 20 units wide, with clipsToBounds set to YES, I'll only see the part of the subview that fits within the bounds of the superview. Otherwise, if clipsToBounds is set to NO, I'll see the entire subview, even the parts outside the superview (assuming we're still on the screen).

As a visual example, consider the following views set up on the storyboard:

This is a white UIView, a label in the top left corner with either a simple "1" or "2" so that I can discuss these as view1 or view2. Additionally, the black view is the same size as the white view, but it's origin is at the white view's center.

In the view controller's viewDidLoad method, we have the following code:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view1.clipsToBounds = YES;
    self.view2.clipsToBounds = NO;
}
override func viewDidLoad() {
    super.viewDidLoad()
    self.view1.clipsToBounds = true
    self.view2.clipsToBounds = false
}

When we run the code and look at in the simulator or on the device, we get the following results:

So, despite these views being set up identically (except clipsToBounds), they look different. This is what clipsToBounds does. Setting it to YES will provide the top result, while setting it to NO provides the bottom result.

If we debug the view hierarchy, we can see more clearly that the black boxes both do actually extend past the boarders of the white view, but only view 2 shows this when the app is actually running:

Fantastic answer! Well done. But I have a question. Why UITextView shadows are not visible, if the clipsToBounds property equal to the true; ? If it's false, works just fine

ios - How does clipsToBounds work? - Stack Overflow

ios uiview uikit frame bounds
Rectangle 27 3

UIView *subview = your View To Be Centered In Its SuperView;

    UIView *superView = subview.superview;

    subview.center = [superView convertPoint:superView.center
                                    fromView:superView.superview];

If view is nil(on fromView:), this method instead converts from window base coordinates. Otherwise, both view and the receiver must belong to the same UIWindow object.

NOTE: If you use the auto layout stuff, then you have to change the constraints . not the frame or center.

swift - ios: Center view in its superview - Stack Overflow

ios swift view position autolayout
Rectangle 27 256

UIView siblings are stacked in the order in which they are added to their superview. The UIView hierarchy methods and properties are there to manage view order. In UIView.h:

@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;

- (void)removeFromSuperview;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

- (void)addSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;

- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;

The sibling views are ordered back to front in the subviews array. So the topmost view will be:

[parentView.subviews lastObject];
[parentView.subviews objectAtIndex:0];

Like Kolin Krewinkel said, [parentView bringSubviewToFront:view] will bring the view to the top, but this is only the case if the views are all siblings in the hierarchy.

ios - How to set iPhone UIView z index? - Stack Overflow

ios uiview position z-index
Rectangle 27 111

UIView is a subclass of UIResponder. UIResponder lays out the method -nextResponder with an implementation that returns nil. UIView overrides this method, as documented in UIResponder (for some reason instead of in UIView) as follows: if the view has a view controller, it is returned by -nextResponder. If there is no view controller, the method will return the superview.

Add this to your project and you're ready to roll.

@interface UIView (APIFix)
- (UIViewController *)viewController;
@end

@implementation UIView (APIFix)

- (UIViewController *)viewController {
    if ([self.nextResponder isKindOfClass:UIViewController.class])
        return (UIViewController *)self.nextResponder;
    else
        return nil;
}
@end
UIView

Shouldn't it be the extension of the UIView class insted of UIViewController class?

This will only work if there is nothing in the responder chain between the receiving UIView and the UIViewController. Phil M's answer featuring recursion is the way to go.

Yes. I've updated the code.

ios - Get to UIViewController from UIView? - Stack Overflow

ios objective-c cocoa-touch uiview uiviewcontroller
Rectangle 27 110

UIView is a subclass of UIResponder. UIResponder lays out the method -nextResponder with an implementation that returns nil. UIView overrides this method, as documented in UIResponder (for some reason instead of in UIView) as follows: if the view has a view controller, it is returned by -nextResponder. If there is no view controller, the method will return the superview.

Add this to your project and you're ready to roll.

@interface UIView (APIFix)
- (UIViewController *)viewController;
@end

@implementation UIView (APIFix)

- (UIViewController *)viewController {
    if ([self.nextResponder isKindOfClass:UIViewController.class])
        return (UIViewController *)self.nextResponder;
    else
        return nil;
}
@end
UIView

This will only work if there is nothing in the responder chain between the receiving UIView and the UIViewController. Phil M's answer featuring recursion is the way to go.

ios - Get to UIViewController from UIView? - Stack Overflow

ios objective-c cocoa-touch uiview uiviewcontroller
Rectangle 27 5

The superview property is not necessarily a strong property and can be a computed property of other private weak property.

The addSubview method establishes a strong reference from the superview to the subview and not necessarily from the subview to the superview.

By the end of the viewDidLoad method view1 is not in the view hirarchy and does not have any other objects pointing to view1 so it gets deallocated but the view2 has also the view controller pointing to it so view2 is not deallocated.

ios - Is UIView superview property weak or strong? - Stack Overflow

ios xcode uiview uikit superview
Rectangle 27 5

The superview property is not necessarily a strong property and can be a computed property of other private weak property.

The addSubview method establishes a strong reference from the superview to the subview and not necessarily from the subview to the superview.

By the end of the viewDidLoad method view1 is not in the view hirarchy and does not have any other objects pointing to view1 so it gets deallocated but the view2 has also the view controller pointing to it so view2 is not deallocated.

ios - Is UIView superview property weak or strong? - Stack Overflow

ios xcode uiview uikit superview
Rectangle 27 32

The best way for me is a category on UIView, but adding views instead of CALayers, so we can take advantage of AutoresizingMasks to make sure borders resize along with the superview.

- (void)addTopBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {
    UIView *border = [UIView new];
    border.backgroundColor = color;
    [border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin];
    border.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth);
    [self addSubview:border];
}

- (void)addBottomBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {
    UIView *border = [UIView new];
    border.backgroundColor = color;
    [border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
    border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);
    [self addSubview:border];
}

- (void)addLeftBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {
    UIView *border = [UIView new];
    border.backgroundColor = color;
    border.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height);
    [border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin];
    [self addSubview:border];
}

- (void)addRightBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {
    UIView *border = [UIView new];
    border.backgroundColor = color;
    [border setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin];
    border.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height);
    [self addSubview:border];
}

This is by far one of the best solutions to this problem. Most of the other solutions offer do NOT honour view changes (and thus device rotation or Split Views). This one does.

ios - How to add a border just on the top side of a UIView - Stack Ove...

ios objective-c xcode uiview border
Rectangle 27 3

The UIView header states that superview property is strong

It doesn't state that at all. What it states is that is property is read-only. You cannot set the superview, so no issue of retention policy even arises. You don't even know whether there is a "superview reference". All you know is that you can ask for the superview by means of property syntax and you will be told what it is. How that is done behind the scenes is not specified and you shouldn't care about it.

Having said that: Obviously a view has no strong reference to its superview, because if it did, there would be a retain cycle (because a superview owns its subview and thus has a strong reference to it).

ios - Is UIView superview property weak or strong? - Stack Overflow

ios xcode uiview uikit superview
Rectangle 27 3

The UIView header states that superview property is strong

It doesn't state that at all. What it states is that is property is read-only. You cannot set the superview, so no issue of retention policy even arises. You don't even know whether there is a "superview reference". All you know is that you can ask for the superview by means of property syntax and you will be told what it is. How that is done behind the scenes is not specified and you shouldn't care about it.

Having said that: Obviously a view has no strong reference to its superview, because if it did, there would be a retain cycle (because a superview owns its subview and thus has a strong reference to it).

ios - Is UIView superview property weak or strong? - Stack Overflow

ios xcode uiview uikit superview
Rectangle 27 25

//You need to specify the frame of the view   
UIView *catView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,400)];

UIImage *image = [UIImage imageNamed:@"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

//specify the frame of the imageView in the superview , here it will fill the superview
imageView.frame = catView.bounds;

// add the imageview to the superview
[catView addSubview:imageView];

//add the view to the main view

[self.view addSubview:catView];

catView doesn't have a view property.

oh yeah, i saw that but forgot to remove it when i commented the code

ios - Adding a UIImage View as a subView to an instance of UIView - St...

ios objective-c uiimageview addsubview
Rectangle 27 25

drawTextInRect works only for 1 line, intrinsicContentSize does not work with horizontal padding. Wrap UILabel inside UIView is the good way to go

If you're in IB, now is the time to remember the menu Editor -> Embed In -> View. Just select your UILabel first :)

ios - Adding space/padding to a UILabel - Stack Overflow

ios uilabel padding
Rectangle 27 2

- (void)resetAll:(id)sender
{
for (UIView *view in self.view.subviews) {
    if ([view isKindOfClass:[StatusView class]]) {
        [view removeFromSuperview];
    }
}
}
- (void)resetAll:(id)sender
{
[sender removeFromSuperview];
}

The first solution won't work as resetAll: method is called on UITableViewCell and it doesn't have the view property

What do you mean when you say self.view.subviews? self is already the cell which is a UIView.

its worked for me. Thanks!for (UIView *view in self.view.subviews) { if ([view isKindOfClass:[StatusView class]]) { [view removeFromSuperview]; } }

ios - Remove Subview from Superview - Stack Overflow

ios uiview subview superview
Rectangle 27 33

UIView convertRect:toView:
CGRect frameRelativeToViewControllerView = [subviewB convertRect:subviewB.bounds toView:viewController.view];

ios - Get view's frame in UIView that is not superview - Stack Overflo...

ios objective-c uiview frame
Rectangle 27 3

A UIView has a superview property and a window property. You can check to see if those are nil. If the view has a nil superview, then it has not been added to anything and is not visible.

If the view does have a superview property that is not nil then you can look at the subviews array property of the superview to determine the stack of views (0 is at the back).

If your view is not at the front of the stack, you would need to look at the frame rects of the other views in front of it to determine if they are covering it. Personally, I would use the CGRectIntersection command to test.

A UIView also has a hidden property which you can check as well as an alpha property which would make the view invisible if it was set to 0.

in my case, CGRectIntersection seems a good idea.

ios - How to check if view, not viewController, is visible or not? - S...

ios objective-c uiview uiviewcontroller
Rectangle 27 3

It looks like from your constraints, that it should work. The problem might be the way you do the animation. You should do that with constraints, not frames. If viewA has a constraint to the bottom of the superview, then you can just animate the constant of that constraint. Make an IBOutlet to that bottom constraint, and do this (bottomCon is the outlet in my example):

self.bottomCon.constant = self.datePicker.bounds.size.height;
[UIView animateWithDuration:2.0 animations:^{
    [self.view layoutSubviews];
    [self.containerView layoutSubviews]; 
} completion:^(BOOL finished) {
    NSLog(@"Done");
}];

In my edit, I also added the [self.containerView layoutSubviews] line. This is necessary so that its subviews are updated properly too (otherwise they just jump to the new location).

ViewA doesn't have a height constraint. How should I do this? ViewA has a Top Space constraint=Superview and a Bottom Space constraint=Superview so that it is flexible for 3.5" and 4" devices.

@KennyWyland, I thought it had a bottom constraint to the date picker not the superview. Is the picker off screen to the bottom?

@KennyWyland, I've edited my answer based on your comment. This should shorten the view by the height of the date picker.

ViewA fills the superview to start and, yes, ViewB is off of the screen at the bottom. ViewA, should initially fill the superview, but when I want to animate the datePicker, ViewB, onto the screen, I want to shorten ViewA to make room for it.

ViewB is connected to the bottom of ViewA, but ViewA isn't connected to the top of B.

ios6 - iOS 6 animate size of view and have constraints move other view...

ios ios6 core-animation autolayout
Rectangle 27 1

I use the following method to remove all constraints from a view:

+ (void)RemoveContraintsFromView:(UIView*)view 
    removeParentConstraints:(bool)parent 
    removeChildConstraints:(bool)child;
+ (void)RemoveContraintsFromView:(UIView *)view 
    removeParentConstraints:(bool)parent 
    removeChildConstraints:(bool)child
{
    if (parent) {
        // Remove constraints between view and its parent.
        UIView *superview = view.superview;
        [view removeFromSuperview];
        [superview addSubview:view];
    }

    if (child) {
        // Remove constraints between view and its children.
        [view removeConstraints:[view constraints]];
    }
}

You can read here how it works behind the hood.

If you need more granular control, I'd strongly advise switching to Masonry, a powerful framework class you could use whenever you need to properly handle constraints programmatically.

ios - Remove all constraints affecting a UIView - Stack Overflow

ios objective-c uiview constraints autolayout
Rectangle 27 2

From my understanding of your Views stack in your viewController, you simply need to move the transparent view to the front of the stack (it is currently at the back of your superView).

You can do this by calling.

And in the storyboard just set the view to the correct values

If you want to hide the view just call

[self.view bringSubViewToFront:<your tableView>];

To create a transparent view on top of you UITableView, you need to add a UIView in the storyBoard that is a subView of your viewController main view(then set all the attributes you want in the attributes inspector). Your tableView need to be also a subView of your viewController main view.

*make sure that the transparent view is front of the tableView in your storyBoard

In order to handle the gestures (because the transparent view is "blocking" the gestures from the tableView read this post Link)

tried it but not working

if I add [self.view bringSubViewToFront:<your transparent view>];then cellForRowAtIndexPath not getting called

can you add your code? from what you described it's not logical to happen

when do you want your transparent view to be on top? from the beginning?

ios - display list of images in UITableview with trasparent view - Sta...

ios objective-c iphone uitableview uiview